yield在多线程中称作放手 主要是当前任务不是特别重要 可以设置为yield
一般的话 设置了yield 线程不会同时进行2个一样的操作 例如写在循环里
class Demo implements Runnable
{
public void run() {
for(int i=0;i<100;i++)
{
System.out.println(Thread.currentThread()+"..."+i);
Thread.yield();
}
}
}
public class Main {
public static void main(String[] args) throws Exception
{
Demo d1=new Demo();
Demo d2=new Demo();
Demo d3=new Demo();
Thread t1=new Thread(d1);
Thread t2=new Thread(d2);
Thread t3=new Thread(d3);
t1.start();
t2.start();
t3.start();
}
}