标签:
1、yield方法
注:yield方法被调用后,并不是让当前线程转入被阻塞状态,而是转入可运行状态
2、创建同优先级的使用yield方法的类
package com.ljb.app.thread;
/**
* 第一个线程(使用yield方法)
* @author LJB
* @version 2015年3月9日
*/
public class OneYield extends Thread{
public void run () {
for (int i = 0 ; i < 5 ; i++) {
System.out.println("oneYield第" + (i+1) + "次运行");
Thread.yield();
}
}
}
package com.ljb.app.thread;
/**
* 第二个线程(使用yield方法)
* @author LJB
* @version 2015年3月9日
*/
public class TwoYield extends Thread{
public void run () {
for (int i = 0 ; i < 5 ; i++) {
System.out.println("twoYield第" + (i+1) + "次运行");
Thread.yield();
}
}
}
2、测试类
package com.ljb.app.thread;
/**
* 测试yield方法
* @author LJB
* @version 2015年3月9日
*/
public class TestYield {
/**
* @param args
*/
public static void main(String[] args) {
Thread oneTh = new OneYield();
Thread twoTh = new TwoYield();
oneTh.start();
twoTh.start();
}
}
运行结果:
oneYield第1次运行
twoYield第1次运行
oneYield第2次运行
twoYield第2次运行
oneYield第3次运行
twoYield第3次运行
oneYield第4次运行
twoYield第4次运行
oneYield第5次运行
twoYield第5次运行
标签:
原文地址:http://my.oschina.net/u/2320342/blog/384345