作者在 2010-05-12 20:44:50 发布以下内容
// 当为true的线程,他执行的时间会返回
public static void main(String[] args) {
Thread t1 = new MyThead(true);
Thread t2 = new MyThead(false);
Thread t3 = new MyThead(true);
t1.setName("第一个");
t2.setName("第二个");
t3.setName("第三个");
t1.start();
t2.start();
t3.start();
}
}
class MyThead extends Thread {
private boolean flag;
public MyThead(boolean flag) {
this.flag = flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void run() {
for (int i = 0; i < 20; i++) {
if (flag) {
Thread.yield();// 当同级线程到达的时候,会让步在这里
}
System.out.println(this.getName());
}
}
}