Concession yield example of java Multithreading notes

Source: Internet
Author: User

Let's take a look at the yield concession example.

The code is as follows: Copy code

Package com. javaer. thread;
 
Public class YThread implements Runnable {
Private Object obj = new Object ();
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
YThread s = new YThread ();
Thread t1 = new Thread (s, "Thread1 ");
Thread t2 = new Thread (s, "Thread2 ");
T1.start ();
T2.start ();
 
 }
 
@ Override
Public void run (){
 
For (int I = 0; I <10; I ++ ){
Try {
Thread. sleep (10 );
} Catch (InterruptedException e ){
 
   }
System. out. println (Thread. currentThread (). getName () + "running" + I );
If (Thread. currentThread (). getName (). equals ("Thread1 ")){
Thread. yield ();
   }
  }
 
 }
 
}
Output

Thread1 is running 0
Thread2 is running 0
Thread1 is running 1
Thread2 is running 1
Thread2 is running 2
Thread1 is running 2
Thread2 is running 3
Thread1 is running 3
Thread2 is running 4
Thread1 is running 4
Thread2 is running 5
Thread1 is running 5
Thread2 is running 6
Thread1 is running 6
Thread2 is running 7
Thread1 is running 7
Thread2 is running 8
Thread1 is running 8
Thread2 is running 9
Thread1 is running 9

We found that, although the system had made concessions since the first cycle, the system actually gave up the resources in the second cycle. In addition, if you execute this code multiple times, it is found that sometimes it is not allowed.

Conclusion

The concession will take the initiative to give up system resources to other threads, but it does not mean that other threads can get CPU resources. In addition, the concession does not affect the execution of the thread. Similar to driving two cars on the road, giving way to others doesn't mean I have to stop.

Let's take a look at the interaction between concessions and locks.

The code is as follows: Copy code
Package com. javaer. thread;
 
Public class YThread implements Runnable {
Private Object obj = new Object ();
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
YThread s = new YThread ();
Thread t1 = new Thread (s, "Thread1 ");
Thread t2 = new Thread (s, "Thread2 ");
T1.start ();
T2.start ();
 
 }
 
@ Override
Public void run (){
 
Synchronized (obj ){
For (int I = 0; I <10; I ++ ){
Try {
Thread. sleep (10 );
} Catch (InterruptedException e ){
 
    }
System. out. println (Thread. currentThread (). getName () + "running" + I );
If (Thread. currentThread (). getName (). equals ("Thread1 ")){
Thread. yield ();
    }
   }
  }
 }
 
}

We know that wait () is used to bring the current thread from the "running state" to the "waiting (blocking) state" and release the synchronization lock. The role of yield () is to make concessions, which will also let the current thread leave the "running state ". Their differences are:
(01) wait () enables the thread to change from "running state" to "Waiting (blocking) state" without yield () is to let the thread from the "running state" to the "ready state ".
(02) wait () means that the thread will release the synchronization lock of the object it holds, while the yield () method will not release the lock.

The above results show that the entire program is locked and executed in sequence.

Thread1 is running 0
Thread1 is running 1
Thread1 is running 2
Thread1 is running 3
Thread1 is running 4
Thread1 is running 5
Thread1 is running 6
Thread1 is running 7
Thread1 is running 8
Thread1 is running 9
Thread2 is running 0
Thread2 is running 1
Thread2 is running 2
Thread2 is running 3
Thread2 is running 4
Thread2 is running 5
Thread2 is running 6
Thread2 is running 7
Thread2 is running 8
Thread2 is running 9
Summary
Yield () serves as a concession. It enables the current thread to change from "running state" to "ready state", so that other waiting threads with the same priority can obtain the execution right. However, it cannot ensure that yield () is called in the current thread () then, other threads with the same priority will be able to obtain the execution right. It may also be that the current thread enters the "running state" to continue running!

1. Only concession
2. The lock will not be released

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.