Java Multithreaded directory:
Java Multithreading-Basic knowledge
Java Multithreading--synchronized Keywords
Java Multithreading--an example of a timed dispatch
Examples of Java multithreading--quartz timing scheduling
Java Multithreading--thread wait and wake
Java Multithreading--Threading concessions
Overview
Part 1th yield () Introduction
Part 2nd yield () example
The comparison of yield () and wait () in the 3rd part
Part 1th 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!
Part 2nd yield () example
1 Packagecom. multithread;2 3 /**4 * 5 * @ClassName: threadb6 * TODO7 * @authorXingle8 * @date 2014-9-19 a.m. 9:19:069 */Ten Public classThreadbextendsthread{ One Publicthreadb (String name) { A Super(name); - } - the Public synchronized voidrun () { - for(inti=0;i<10;i++){ -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 (); + } A } at}
1 Packagecom. multithread;2 3 /**4 * 5 * @ClassName: Yieldtest6 * TODO7 * @authorXingle8 * @date 2014-9-19 a.m. 9:36:599 */Ten Public classYieldtest { One A Public Static voidMain (string[] args) { -threadb T1 =NewTHREADB ("T1"); -THREADB t2 =NewTHREADB ("T2"); the T1.start (); - T2.start (); - } - +}
Execution Result:
Result Description :
"Thread T1" does not switch to "thread T2" when it can be 4 integers. This indicates that yield () can allow a thread to go from "Running state" to "ready state", but it does not necessarily allow other threads to get CPU execution (that is, other threads go into "running state"), even if this "other thread" has the same priority as the thread that is currently calling yield ().
The comparison of yield () and wait () in the 3rd part
The role of Wait () is to allow the current thread to enter the "waiting (blocking) state" from the "Running state" while also releasing 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.
The following example shows that yield () does not release the lock.
1 Packagecom. multithread;2 3 /**4 * 5 * @ClassName: Yieldlocktest6 * TODO7 * @authorXingle8 * @date 2014-9-19 a.m. 9:47:249 */Ten Public classYieldlocktest { One A Private StaticObject obj =NewObject (); - - Public Static voidMain (string[] args) { theThread1 T1 =NewThread1 ("T1"); -Thread1 t2 =NewThread1 ("T2"); - T1.start (); - T2.start (); + - } + A Static classThread1extendsThread { at - PublicThread1 (String name) { - Super(name); - } - - Public voidrun () { in synchronized(obj) { - for(intI= 0;i<10;i++){ toSystem.out.printf ("%s [%d]:%d\n", This. GetName (), This. GetPriority (), i); + if(i%4==0) - Thread.yield (); the } * $ }Panax Notoginseng } - the } + A}
Execution Result:
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"!
Java Multithreading--Threading concessions