Multithreading - Life Cycle

This article is reproduced -  https://www.cnblogs.com/marsitman/p/11228684.html

Thread life cycle consists of five stages, including: new, ready, running, blocking, destruction.

  • New: is just used new methods, new out of the thread;

  • Ready: that start () method is called after the thread, this time the thread is waiting for CPU resource allocation stage, the first to grab the CPU resources, who started;

  • Run: When ready thread is scheduled and access to CPU resources, it went into operation, run method defines the operations and functions of the thread;

  • Blocked: running state, it may cause the thread to run for some reason the state into a blocking state, such as after sleep (), wait () thread is in a blocked state, this time the need for other mechanisms will be in the blocked state thread wakes, such as calling notify or notifyAll () method. Awakened thread run method will not be executed immediately, they wait for CPU resource allocation into operation again;

  • Destruction: If the thread or threads are normally finished ahead of the mandatory termination or abnormal lead to an end, the thread will be destroyed, the release of resources;

Complete life cycle is as follows:

                                

 

New Status

We look at the following piece of code:

1
Thread t1 =  new  Thread();

Created here, just to be created in this programming language JAVA level, while at the operating system level, the real thread has not been created. Only when we call the start () method, the thread will be created out into Runnable state. Only when we call the start () method, the thread will be created out

                                                                                      

 

Ready

After calling start () method, JVM process will be to create a new thread, and this thread will not immediately be scheduled to run CPU, enter the Running state, there will be an intermediate state, is Runnable state, you can wait to be understood as a CPU scheduling status

1
t1.start()

Represented by a diagram as follows:

Then the thread is Runnable state transition can occur which state?

Runnable状态的线程无法直接进入Blocked状态和Terminated状态的。只有处在Running状态的线程,换句话说,只有获得CPU调度执行权的线程才有资格进入Blocked状态和Terminated状态,Runnable状态的线程要么能被转换成Running状态,要么被意外终止。

 

运行状态

当CPU调度发生,并从任务队列中选中了某个Runnable线程时,该线程会进入Running执行状态,并且开始调用run()方法中逻辑代码。

那么处于Running状态的线程能发生哪些状态转变?

  • 被转换成Terminated状态,比如调用 stop() 方法;

  • 被转换成Blocked状态,比如调用了sleep, wait 方法被加入 waitSet 中;

  • 被转换成Blocked状态,如进行 IO 阻塞操作,如查询数据库进入阻塞状态;

  • 被转换成Blocked状态,比如获取某个锁的释放,而被加入该锁的阻塞队列中;

  • 该线程的时间片用完,CPU 再次调度,进入Runnable状态;

  • 线程主动调用 yield 方法,让出 CPU 资源,进入Runnable状态

 

阻塞状态

Blocked状态的线程能够发生哪些状态改变?

  • 被转换成Terminated状态,比如调用 stop() 方法,或者是 JVM 意外 Crash;

  • 被转换成Runnable状态,阻塞时间结束,比如读取到了数据库的数据后;

  • 完成了指定时间的休眠,进入到Runnable状态;

  • 正在wait中的线程,被其他线程调用notify/notifyAll方法唤醒,进入到Runnable状态;

  • 线程获取到了想要的锁资源,进入Runnable状态;

  • 线程在阻塞状态下被打断,如其他线程调用了interrupt方法,进入到Runnable状态;

 

终止状态

一旦线程进入了Terminated状态,就意味着这个线程生命的终结,哪些情况下,线程会进入到Terminated状态呢?

  • 线程正常运行结束,生命周期结束;

  • 线程运行过程中出现意外错误;

  • JVM 异常结束,所有的线程生命周期均被结束。

 

Guess you like

Origin www.cnblogs.com/123-shen/p/11229612.html