可以轮询
也可以起另一个线程去控制
可惜不是你,陪我到最后
/*
* Test2.java
*
* Created on 2006年11月21日, 下午8:10
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package myTest;
/**
*
* @author lbf
*/
public class Test2 extends Thread{
private MyThread my1,my2;
private volatile boolean my1Running;
/** Creates a new instance of Test2 */
public Test2() {
initOther();
}
private void initOther(){
my1=new MyThread("线程1");
my2=new MyThread("线程2");
my1.start();
my2.start();
this.start();
}
public void run(){
while(true){
try{
changeTask();
Thread.sleep(10000);
}
catch(Exception exe){
exe.printStackTrace();;
}
}
}
private void changeTask(){
System.out.println("换任务了=======");
if(my1Running){
my1.pause();
my2.resumes();
my1Running=false;
}else{
my1.resumes();
my2.pause();
my1Running=true;
}
}
//你要处理事件的线程类
private class MyThread extends Thread{
volatile boolean go;
int total;
public MyThread(String name){
super(name);
}
public void run(){
while(true){
try{
if(go){
System.out.println("线程"+this.getName()+":"+total++);
Thread.sleep(500);
}else{
Thread.sleep(10);
}
}
catch(Exception exe){
exe.printStackTrace();
}
}
}
public void pause(){
go=false;
}
public void resumes(){
go=true;
}
}
public static void main(String[] args) {
new Test2();
}
}
你看看吧,不知道是 不是你想要的样子