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();
}
}
}
}
通过Interface的Runnable实现多线程的Yield,含setName,getName
最新推荐文章于 2022-06-17 18:03:40 发布