首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

求问线程控制有关问题,多谢各位大侠

2011-12-22 
求问线程控制问题,谢谢各位大侠。import java.io.*public class testThread {class ThreadA extends Threa

求问线程控制问题,谢谢各位大侠。
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

热点排行