线程让步

来源:互联网 发布:现代网络语言的英文 编辑:程序博客网 时间:2024/05/16 01:59

Thread.yield()方法会暂停当前正在执行的线程对象,把执行机会让给相同或者更高优先级的线程

public class ThreadYieldTest {
 public static void main(String[] args) {
  System.out.println(Thread.currentThread().getName());
  Thread thread1=new Thread(new YieldThread());
  thread1.start();
  Thread thread2=new Thread(new YieldThread());
  thread2.start();
 }
}

class YieldThread implements Runnable{

 public void run() {
  for(int i=0;i<100;i++){
   System.out.println(Thread.currentThread().getName()+":"+i);
   if(i%10==0){
    Thread.yield();
   }
  }
 }
 
}

 

0 0