public class MainThread extends Thread {
public void run() {
System.out.println(" mainThread start");
try {
MyThread thread = null;
for (int i = 0; i < 5; i++) {
thread= new MyThread(""+i);
thread.start();
}
// thread.join();
} catch (Exception e) {
e.printStackTrace();
} }
}
这个是主线程public class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name = name;
} public void run(){
for(int i=0; i<100; i++){
System.out.println(" mythread :"+name+": "+i);
}
}
}
这个是子线程主线程怎么样写才能等待子线程全部执行完再结束主线程?
public void run() {
System.out.println(" mainThread start");
try {
MyThread thread = null;
for (int i = 0; i < 5; i++) {
thread= new MyThread(""+i);
thread.start();
}
// thread.join();
} catch (Exception e) {
e.printStackTrace();
} }
}
这个是主线程public class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name = name;
} public void run(){
for(int i=0; i<100; i++){
System.out.println(" mythread :"+name+": "+i);
}
}
}
这个是子线程主线程怎么样写才能等待子线程全部执行完再结束主线程?
public subThreadCount = 0;
run(){
//启动子线程
并且改变subThreadCount的值
while (true){
if (subThreadCount == 0){
break;
} else{
sleep(1000);
}
}
}
子线程:
每个run()执行完了就把MainThread.subThreadCount--;