Description :
The thread altogether consists of the following 5 states.
1. new state : When the thread object is created, it enters the new state. For example, thread thread = new Thread ().
2. ready State (Runnable): Also known as "executable State". When the thread object is created, the other thread invokes the object's start () method to start the thread. For example, Thread.Start (). A thread that is in a ready state may be executed by the CPU at any time.
3. running State (Running) : Thread gets CPU permission to execute. It is important to note that a thread can only go from a ready state to a running state.
4. blocking State (Blocked) : The blocking state is a temporary stop for a thread that has abandoned the CPU usage for some reason. Until the thread is in a ready state, the opportunity to go to the running state is reached. There are three types of blocking:
(01) Wait for blocking--by calling the thread's Wait () method, to let the thread wait for the completion of a job.
(02) Synchronization blocking--the thread acquires a synchronized synchronization lock failure (because the lock is occupied by another thread), it goes into a synchronous blocking state.
(03) Other blocking-the thread enters the blocking state by calling the thread's sleep () or join () or making an I/O request. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state.
5. Death Status (Dead) : The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle.
1. Wait (), notify (), Notifyall () and other methods introduced
In Object.java, interfaces such as Wait (), notify (), and notifyall () are defined. The role of Wait () is to let the current thread go into a wait state, and wait () also causes the current thread to release the locks it holds. The role of Notify () and Notifyall () is to wake up the waiting thread on the current object, notify () is to wake up a single thread, and Notifyall () is to wake up all the threads.
The following API details about wait/wake in the object class are as follows:
Notify () --wakes up a single thread waiting on this object monitor.
Notifyall () --wakes up all the threads waiting on this object monitor.
Wait () --keeps the current thread in a "waiting (blocking) state" until other threads call the Notify () method of this object or the Notifyall () method, which is currently The thread is awakened (into ready state).
Wait (long timeout) --Leave the current thread in a "waiting (blocking) state" until another thread calls the Notify () method of this object or the Notifyall () method, or exceeds the specified amount of time ", The current thread is awakened (into ready state).
Wait (long timeout, int nanos) --keeps the current thread in a "waiting (blocking) state" until another thread calls the Notify () method of this object or the Notifyall () method, or some other thread interrupts the current thread. Or has exceeded an actual amount of time ", the current thread is awakened (into the ready state).
2. Why notify (), wait () functions are defined in object, not thread
The Wait (), notify () functions in object, like synchronized, operate on the "object's synchronous lock."
Wait () causes the "current thread" to be waiting because the thread goes into a wait state, so the thread should free its lock-held "sync lock", otherwise the other thread will not get the "sync lock" to run!
OK, after the thread calls wait (), it releases the "Sync Lock" held by the lock, and, according to the previous introduction, we know that the waiting thread can be awakened by notify () or Notifyall (). Now, consider a question: Notify () is based on what wakes the waiting thread? Or, what is the connection between wait () waiting thread and notify ()? The answer is: according to the "synchronization lock of the object".
The thread responsible for waking the waiting thread (what we call a " wake-up Thread ") is only getting "the object's Sync lock" ( where the sync lock must be the same as the waiting thread's Sync lock ), and calls notify () or Notifyall () method to wake the waiting thread. Although the wait thread is awakened, it cannot be executed immediately, because the wake-up thread also holds "the object's synchronization lock." You must wait until the wake-up thread frees the "object's synchronization lock," and wait for the thread to get to the "object's synchronization lock" and continue to run.
In summary, notify (), wait () depends on "sync lock", and "Sync Lock" is Object lock held, and each object has and only one! This is why notify (), wait () functions are defined in the object class, not in the thread class.
3. Yield () Introduction
Yield () is the function of concession. It allows the current thread to enter the "Ready state" from the "running state", allowing other waiting threads with the same priority to get execution, but there is no guarantee that other threads with the same priority will be able to gain execution after the current thread calls yield (), or that the current thread is entering the " Run status "continue running!
4. Yield () comparison to wait ()
We know that the role of Wait () is to allow the current thread to enter the "waiting (blocking) state" from the "Running state" and also release the synchronization lock. Yield () is a concession, and it also leaves the current thread out of the "running state". The difference between them is:
Wait () is a thread that enters the "waiting (blocking) state" from the "running state" and does not yield () to allow the thread to go from "Running state" to "ready state".
Wait () is the thread that releases the synchronization lock on the object it holds, and the yield () method does not release the lock.
Yieldlocktest.java source public class yieldlocktest{ private static Object obj = new Object (); public static void Main (string[] args) { Threada t1 = new Threada ("T1"); Threada t2 = new Threada ("T2"); T1.start (); T2.start (); } Static class Threada extends thread{public Threada (String name) { super (name); } public void Run () { //Gets the synchronization lock synchronized (obj) {for (int i=0; I <10; i++) of the Obj object { System.out.printf ("%s [%d]:%d\n", This.getname (), this.getpriority (), i); I divide 4 o'clock, call yield if (i%4 = = 0) Thread.yield () ; } }}}
Run results (one time) :
T1 [5]:0T1 [5]:1T1 [5]:2T1] [5]:3T1 [5]:4T1] [5]:5T1] [5]:6T1 [5]:7T1 [5]:8T1] [5]:9T2 [5]:0T2 [5]:1t2] [5]:2t2 [5]:3T2] [5]:4t2] [5]:5t2 [5]:6t2 [5]:7T2 [5]:8t2] [5]:9
Result Description :
The main thread, main thread, starts with two threads T1 and t2. T1 and T2 in run () refer to the synchronization lock of the same object, that is, synchronized (obj). While the T1 is running, it will call Thread.yield (), but T2 will not get CPU execution. Because, T1 did not release "the synchronization lock held by obj"!
5. Sleep () Introduction
Sleep () is defined in Thread.java.
The function of sleep () is to let the current thread hibernate, that is, the current thread goes from "Running state" to "hibernate (blocked) state". Sleep () Specifies the sleep time at which the thread sleeps longer than/equal to the sleep time, and when it is woken up, it is changed from "blocked state" to "ready state", thus waiting for the CPU to schedule execution.
6. Comparison of sleep () and wait ()
We know that the role of Wait () is to allow the current thread to enter the "waiting (blocking) state" from the "Running state" and also release the synchronization lock. The function of sleep () is to let the current thread go into hibernation (blocked) state by the "Running state".
However, wait () frees the object's synchronization lock, and sleep () does not release the lock.
The following example shows that sleep () does not release the lock.
Sleeplocktest.java source public class sleeplocktest{ private static Object obj = new Object (); public static void Main (string[] args) { Threada t1 = new Threada ("T1"); Threada t2 = new Threada ("T2"); T1.start (); T2.start (); } Static class Threada extends thread{public Threada (String name) { super (name); } public void Run () { //Gets the Obj object's synchronization lock synchronized (obj) { try {for (int i=0; I <10; i++) { System.out.printf ("%s:%d\n", This.getname (), i); I can be divisible by 4, sleep 100 milliseconds if (i%4 = = 0) thread.sleep ()} } catch (Interruptedexception e) { E.printstacktrace ();}}} }
The main thread, main thread, starts with two threads T1 and t2. T1 and T2 in run () refer to the synchronization lock of the same object, that is, synchronized (obj). While the T1 is running, it calls Thread.Sleep (100), but T2 does not get CPU execution. Because, T1 did not release "the synchronization lock held by obj"!
Note that if we comment out synchronized (obj) and execute the program again, T1 and T2 can switch to each other.
Java thread state Flow---thread