public class YieldTest {
/**
* @param args
*/
public static void main(String[] args) {
Test6 test = new Test6("1234");
test.start();
for(int i =0 ;i< 8;i++){
System.out.println("main"+i);
while(i==3){
Thread.yield();//暂停
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Test6 extends Thread{
public Test6(String s){
super(s);
}
@Override
public void run() {
for(int i =0;i<6;i++){
System.out.println("线程"+getName()+"正在执行");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
结果:
main0
线程1234正在执行
main1
main2
main3
线程1234正在执行
线程1234正在执行
线程1234正在执行
线程1234正在执行
线程1234正在执行