日期:2014-05-18  浏览次数:20683 次

求问线程控制问题,谢谢各位大侠。
import java.io.*;
public class testThread {

class ThreadA extends Thread {
boolean runFlag = true;
int count = 0;

public void run(){
while(runFlag){
System.out.println(Integer.toString(count++));
if(count == 10){
return;
}
}// endwhile
}// endrun()
}//

class ThreadB extends Thread{
boolean runFlag = true;
public void run(){
while(runFlag){
System.out.println("*** run thread b ***");
return;
}//endwhile
}//endrun()
}

public void runTest(){
ThreadA a= new ThreadA();
ThreadB b = new ThreadB();
a.start();
b.start();
}

public static void main(String[] args){
testThread tt = new testThread();
tt.runTest();
  System.out.println(“*** program end ***”);
}
}


输出为:
*** progream end ***
*** run thread b ***
0
1
2
3
4
5
6
7
8
9
如果我要得到的输出为
0
1
2
3
4
5
6
7
8
9
*** run thread b ***
*** progream end ***
应当怎样控制线程?谢谢各位


------解决方案--------------------
use join method

public void runTest(){ 
ThreadA a= new ThreadA(); 
ThreadB b = new ThreadB(); 
a.start(); 
a.join(); // here
b.start();
b.join(); // here