A thread is in a variety of different states during its life cycle: new, waiting, ready, running, blocking, and dying.
1. New
The thread object created with the new statement is in the new state, which, like other Java objects, is allocated only memory.
2. Wait
When the thread is behind new, and before the Start method is called, the thread is in a wait state.
3. Ready
When a thread object is created, the other thread calls its start () method, and the thread enters the ready state. The thread in this state is in the running pool of the Java Virtual machine and waits for the CPU to use it.
4. Operation Status
The thread in this state consumes the CPU and executes the program code. In a concurrent runtime environment, if the computer has only one CPU, only one thread will be in this state at any time.
Only threads in a ready state have the opportunity to go to the running state.
5. Blocking status
A blocking state is when a thread abandons the CPU for some reason and temporarily stops running. When a thread is in a blocking state, the Java Virtual Machine does not allocate the CPU to the thread until the thread is back in the ready state before it has a chance to get a running state.
6. Death status
When the thread finishes executing the code in the Run () method, or encounters an uncaught exception, it exits the run () method and then enters the dead state, which ends the life cycle.
Because Java thread scheduling is not time-sharing, if the program wants to intervene in the Java virtual machine to the thread of the scheduling process, thus explicitly let a thread to another thread to run the opportunity, you can use the following methods
1. Adjust the priority of each thread
2. Let the running thread call the Thread.Sleep (long time) method to discard the CPU into the blocking state
The Sleep method may throw interruptedexception
After a thread sleeps, it can only leave the thread in a ready state after a specified time. (that is, waiting for the CPU to dispatch)
3. Let the running thread call the Thread.yield () method, only with priority concessions or higher priority concessions (into the ready state)
4. Let the running thread call the Join () method of another thread
The currently running thread can invoke the join () method of another thread, and the currently running thread will go to the blocking state until another thread finishes running before it goes to the ready state for a chance to resume running.
Through one of several ways, a thread can be from a blocked state to a running state.
1. The thread is put to sleep, and the specified number of milliseconds has elapsed.
2. The thread is waiting for the I/O operation to complete and the operation has completed.
3. The thread is waiting for a lock held by another thread, and the other thread has already freed ownership of the lock, or it may wait for a timeout. When the timeout occurs, the thread is unblocked. )
4. The thread is waiting for a trigger condition and another thread signals that the condition has changed. (If a timeout is set for the thread's wait, the thread is unblocked when the timeout occurs.) )
5. The thread has been suspended and someone has called its resume method. However, since the Suspend method is obsolete, the resume method is deprecated, and you should not call it in your own code. (It should now be replaced with sleep.) )
Massive video sharing Java Dubbo Spring
Thread various state transition analysis