JAVA的线程让步

JAVA的线程让步yield的使用方法:

 1 package suspend;
 2 public class Machine extends Thread{
 3   private int a;  //共享数据
 4 
 5   public void run(){
 6     for(int i=0;i<1000;i++){
 7       synchronized(this){
 8        a+=i;
 9        yield();  //给其他线程运行的机会
10        a-=i;
11      }
12     }
13   }
14 
15   public synchronized void reset(){ a=0;}
16 
17   public static void main(String args[]) throws InterruptedException{
18     Machine machine=new Machine();
19     machine.start();
20     yield();  //给machine线程运行的机会
21     machine.suspend();  //让machine线程暂停
22     machine.reset();  //调用machine对象的同步代码块
23     machine.resume();  //使machine线程恢复运行
24   }
25 }
原文地址:https://www.cnblogs.com/djcsch2001/p/2486615.html