[Java] [Multithreading] The life cycle of a thread

 

Reprinted: https://blog.csdn.net/miss_dongangel/article/details/51195337

very comprehensive


1. New state

   

     After a thread object is created by inheriting the Thread class or implementing the Runable interface or other methods, the thread object is in a new state. The thread in the new state has its own memory space and enters the ready state ( runnable ) by calling the start method .

    

Note: The start() method cannot be called again on a thread that has already been started, otherwise a java.lang.IllegalThreadStateException will occur.


2. Ready state


    Threads in the ready state are ready to run, but have not been allocated to the CPU , and are in the thread ready queue (although in the form of a queue, in fact, it is called a runnable pool rather than a runnable queue. Because the cpu scheduling does not It must be scheduled in a first-in, first-out order), waiting for the system to allocate a CPU to it. The waiting state is not the execution state. When the system selects a Thread object waiting to be executed, it will enter the running state from the waiting running state . The action selected by the system is called "cpu scheduling ". Once the CPU is obtained, the thread enters the running state and automatically calls its own run method.


Tip: If you want the child thread to execute immediately after calling the start() method, you can use the Thread.sleep() method to make the main thread sleep for a while, and then go to execute the child thread.


3. Running status

    

    A running thread is the most complex, it can become blocked, ready and dead.

    If the thread in the ready state is scheduled by the CPU, it will change from the ready state to the running state and execute the tasks in the run() method. If the thread loses cpu resources, it will change from the running state to the ready state again. Wait for the system to allocate resources again. You can also call the yield() method on the thread in the running state, it will give up the cpu resources and become the ready state again.


    A thread changes from a running state to a blocked state when:

1. The thread calls the sleep method to actively give up the occupied system resources and give up the CPU

2. The thread calls a blocking IO method, and the thread is blocked before the method returns

3. The thread tries to get a synchronization monitor, but the change synchronization monitor is being held by other threads

④. After the thread uses the wait() method to give up system resources, it is waiting for a notification (notify),

⑤, the program calls the thread's suspend method to suspend the thread. However, this method is easy to cause deadlock, so the program should try to avoid using this method.


    When the run() method of the thread is executed, or is forcibly terminated, such as an exception occurs, or the stop(), desyory() methods, etc. are called, it will change from the running state to the dead state.


4. Blocking state


    In some cases, a thread in the running state, such as executing the sleep method or waiting for resources such as I/O devices, will give up the CPU and temporarily stop its own operation and enter a blocking state. 

    A thread in a blocked state cannot enter the ready queue. Only when the cause of the blocking is eliminated, such as the sleep time has expired, or the waiting I/O device is idle, the thread will transfer to the ready state, re-queue in the ready queue, and start from the original stop position after being selected by the system Keep running. There are three ways to pause Threads execution:


5. The state of death


    当线程的run()方法执行完,或者被强制性地终止,就认为它死去。这个线程对象也许是活的,但是,它已经不是一个单独执行的线程。线程一旦死亡,就不能复生。 如果在一个死去的线程上调用start()方法,会抛出java.lang.IllegalThreadStateException异常。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324578506&siteId=291194637