例子十三.txt
来自「这是一本java基础教程 对新手上路有很大帮助」· 文本 代码 · 共 36 行
TXT
36 行
public class Example8_13{
public static void main(String args[ ]){
JoinThread a=new JoinThread ();
a.threadA.start();
}
}
class JoinThread implements Runnable{
Thread threadA,threadB;
String content[ ]={"今天晚上,","大家不要","回去的太早,","还有工作","需要大家做!"};
JoinThread(){
threadA=new Thread(this);
threadB=new Thread(this);
threadB.setName("经理");
}
public void run(){
if(Thread.currentThread()==threadA){
System.out.println("我等"+threadB.getName()+"说完再说话");
threadB.start();
while(threadB.isAlive()==false){}
try{ threadB.join(); //线程threadA开始等待threadB结束
}
catch(InterruptedException e){}
System.out.printf("\n我开始说话:\"我明白你的意思了,谢谢\"");
}
else if(Thread.currentThread()==threadB){
System.out.println(threadB.getName()+"说:");
for(int i=0;i<content.length;i++){
System.out.print(content[i]) ;
try { threadB.sleep(1000);
}
catch(InterruptedException e){}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?