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 ***
应当怎样控制线程?谢谢各位
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 ***
应当怎样控制线程?谢谢各位
class ThreadA extends Thread {
private Thread t=new ThreadB();
boolean runFlag = true;
int count = 0; public void run(){
while(runFlag){
System.out.println(Integer.toString(count++));
if(count == 10){
t.join();
return;
}
}
}
}
class ThreadB extends Thread{
boolean runFlag = true;
public void run(){
while(runFlag){
System.out.println("*** run thread b ***");
return;
}
}
} public void runTest(){
ThreadA a= new ThreadA();
a.start();
} public static void main(String[] args){
testThread tt = new testThread();
tt.runTest();
System.out.println(“*** program end ***”);
}
}
//////代码不一定对,原理是这样,t1运行到何种程度,然后 t2 join 打断t1的运行,直到t2运行结束
2死循环判断t1.isAlive(),比较耗资源
3yield() 同1类似
上面所述比较垃圾,随便看吧呵呵!
class ThreadA extends Thread {
private Thread t=new ThreadB();
boolean runFlag = true;
int count = 0; public void run(){
while(runFlag){
System.out.println(Integer.toString(count++));
if(count == 10){
t.join();
return;
}
}
}
}
class ThreadB extends Thread{
boolean runFlag = true;
public void run(){
while(runFlag){
System.out.println("*** run thread b ***");
return;
}
}
} public void runTest(){
ThreadA a= new ThreadA();
a.start();
} public static void main(String[] args){
testThread tt = new testThread();
tt.runTest();
System.out.println(“*** program end ***”);
}
}
//////代码不一定对,原理是这样,t1运行到何种程度,然后 t2 join 打断t1的运行,直到t2运行结束
2死循环判断t1.isAlive(),比较耗资源
3yield() 同1类似
上面所述比较垃圾,随便看吧呵呵!
ThreadA a= new ThreadA();
ThreadB b = new ThreadB();
a.start();
a.jion(); //here
b.start();
b.jion(); //here
}