classTestThread1publicstaticvoidmain(String[]args){System.out.println("2个线程开始执行....");TestHellohello1=newTestHello("A",1000);TestHellohello2=newTestHello("B",2000)...
class TestThread1
public static void main(String[] args) {
System.out.println("2个线程开始执行....");
TestHello hello1 = new TestHello("A",1000);
TestHello hello2 = new TestHello("B",2000);
Thread demo1 = new Thread(hello1);
Thread demo2 = new Thread(hello2);
demo1.start();
demo2.start();
System.out.println("线程均执行完成....");
}
=======================================
class TestHello
private String workname;
private int worktime;
//省略 workname和worktime的set和get方法
public TestHello(String workname, int worktime) {
super();
this.workname = workname;
this.worktime = worktime;
}
public void run(){
for (int i = 0; i < 5; i++){
System.out.println(this.workname + "运行 " + i );
try {
Thread.sleep(worktime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
=======================================
想在启动线程后加锁 等待两个线程一起完成 再输出最后一句话
怎么实现
代码若有可以完善的地方 可以告诉我 新手 求指教
我找到解决办法了
public static void main(String[] args) {
......
demo1.start();
demo2.start();
//******************************************
try {
demo1.join();
demo2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//******************************************
System.out.println("线程均执行完成....");
}
展开