Start with the picture first
A little explanation:
1, the implementation of the thread has two ways, one is to inherit the thread class, the second is to implement the Runnable interface, but anyway, when we new this object, the thread entered the initial state;
2, when the object calls the start () method, it enters the operational state;
3, after entering the operational state, when the object is selected by the operating system, the CPU time slice will enter the running state;
4. After entering the operating state, the situation is more complicated.
4.1, the Run () method or the main () method ends, the thread will enter the terminating state;
4.2. When a thread calls its own sleep () method or the Join () method of another thread, it enters a blocking state (which stops the current thread but does not release the resources it occupies). When sleep () ends or join () ends, the thread enters the operational state and continues to wait for the OS to allocate time slices;
4.3, the thread calls the yield () method, which means to discard the current CPU time slice, back to the operational state, and other processes in the same competitive state, the OS may then let the process into the running State;
4.4, when the thread has just entered the operational state (note, not yet running), found that the resource to be called is Synchroniza (synchronous), get not the lock tag, will immediately enter the lock pool state, waiting for the lock tag (in this case, the lock pool may already have other threads waiting to get the lock tag, At this point they are in the queue state, on a first-come-first-served basis, once the thread obtains the lock tag, it goes to the operational state, waiting for the OS to allocate CPU time slice;
4.5, when the thread calls the Wait () method will enter the waiting queue (enter this state will release all the resources occupied, and the blocking state is different), after entering this state, is not automatically wake up, you must rely on other threads call the Notify () or Notifyall () method to be awakened ( Since notify () only wakes up a thread, we are not sure which thread is specifically awakened, perhaps the thread we need to wake up is not able to wake up, so in practice it is generally used with the Notifyall () method, which wakes up some threads), and the thread is awakened and enters the lock pool. Wait for the lock token to be acquired.
It's all over again. JDK1.5 in the use of the API has a good improvement, efficiency has been greatly improved, but a few state conversion principle is the same.
"Go" thread state