Java basic tutorial thread concession Java multithreading tutorial

  • 2020-04-01 02:43:56
  • OfStack

The contents covered in this chapter include:
1. The yield () is introduced
2. The yield () sample
3. Yield () versus wait()

1. The yield () is introduced
Yield () yields. It can make the current thread from the "run state" to the "ready state", so that other waiting threads with the same priority to get the execution right; However, there is no guarantee that after the current thread invokes yield(), other threads with the same priority will get execution rights. It is also possible that the current thread has entered the "running state" to continue running!

2. The yield () sample
Next, see how it is used through an example.


//YieldTest. Java source code
class ThreadA extends Thread{
    public ThreadA(String name){ 
        super(name); 
    } 
    public synchronized void run(){ 
        for(int i=0; i <10; i++){ 
            System.out.printf("%s [%d]:%dn", this.getName(), this.getPriority(), i); 
            //When I divisible by 4, yield is called
            if (i%4 == 0)
                Thread.yield();
        } 
    } 
} 
public class YieldTest{ 
    public static void main(String[] args){ 
        ThreadA t1 = new ThreadA("t1"); 
        ThreadA t2 = new ThreadA("t2"); 
        t1.start(); 
        t2.start();
    } 
}

Result of operation:


t1 [5]:0
t2 [5]:0
t1 [5]:1
t1 [5]:2
t1 [5]:3
t1 [5]:4
t1 [5]:5
t1 [5]:6
t1 [5]:7
t1 [5]:8
t1 [5]:9
t2 [5]:1
t2 [5]:2
t2 [5]:3
t2 [5]:4
t2 [5]:5
t2 [5]:6
t2 [5]:7
t2 [5]:8
t2 [5]:9

Results:
"Thread t1" does not switch to "thread t2" when it can be 4 integers. This shows that yield() can move a thread from "run" to "ready"; However, it does not necessarily give other threads access to CPU execution (that is, other threads enter "run state"), even if this "other thread" has the same priority as the current thread that calls yield().

3. Yield () versus wait()
We know that wait() releases the synchronization lock as it moves the current thread from "run" to "wait". Yield () yields, which also takes the current thread out of "run state." The difference is:
(01) wait() is for threads to go from "run" to "wait(blocked)", and not yield() is for threads to go from "run" to "ready".
(02) wait() releases a synchronized lock on an object held by a thread, while the yield() method does not.

The following example demonstrates that yield() does not release locks.


//YieldLockTest. Java source code
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 for the obj object
            synchronized (obj) {
                for(int i=0; i <10; i++){ 
                    System.out.printf("%s [%d]:%dn", this.getName(), this.getPriority(), i); 
                    //When I divisible by 4, yield is called
                    if (i%4 == 0)
                        Thread.yield();
                }
            }
        } 
    } 
}

Operation result:


t1 [5]:0
t1 [5]:1
t1 [5]:2
t1 [5]:3
t1 [5]:4
t1 [5]:5
t1 [5]:6
t1 [5]:7
t1 [5]:8
t1 [5]:9
t2 [5]:0
t2 [5]:1
t2 [5]:2
t2 [5]:3
t2 [5]:4
t2 [5]:5
t2 [5]:6
t2 [5]:7
t2 [5]:8
t2 [5]:9

Results:
Two threads, t1 and t2, are started in the main thread. T1 and t2 refer to synchronized(obj), a synchronized lock on the same object, in run(). While t1 is running, it calls thread.yield (); However, t2 does not get CPU execution. Because, t1 does not release the synchronization lock held by obj!


Related articles: