您的位置:首页 > 其它

通过Interface的Runnable实现多线程的Yield,含setName,getName

2016-05-10 21:09 471 查看
public class TestThreadYield {

public static void main(String[] args) {
SubThread subThread_1 = new SubThread("subThread_1");
SubThread subThread_2 = new SubThread("subThread_2");
Thread th1 = new Thread(subThread_1);
Thread th2 = new Thread(subThread_2);
th1.start();
th2.start();
}
}

class SubThread implements Runnable{
private String name;
SubThread(String str){
name = str;
}

public void run(){
Thread.currentThread().setName(name);
for(int i = 0; i <= 100; i++){
System.out.println(Thread.currentThread().getName() + ": " + i);
if(i%10 == 0){
Thread.currentThread();
Thread.yield();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息