public class TestThreadJoin {
public static void main(String[] args) {
SubThread sub = new SubThread("AllByMyself");
Thread hst = new Thread(sub);
hst.start();
try{
hst.join();
}catch(InterruptedException ex){
}
for(int i = 0; i < 10; i++){
System.out.println("This is HOST Thread");
}
}
}
class SubThread implements Runnable{
private String sname;
SubThread(String str){
sname = str;
}
public void run(){
Thread.currentThread().setName(sname);
for(int i = 0; i < 10; i++){
System.out.println("I am " + Thread.currentThread().getName());
try{
Thread.currentThread();
Thread.sleep(1000);;
}catch(InterruptedException e){
return;
}
}
}
}
通过interface的Runnable实现线程合并(join),涉及setName,getName,sleep
最新推荐文章于 2022-06-01 16:16:40 发布