我太菜了 多多指教 这样写行不?import java.lang.Thread; public class Test{ public static void main(String[] args){ Mon mon = new Mon(); mon.start(); } }class Send extends Thread{
public void run(){ System.out.println("---send"); } }class Get extends Thread{
public void run(){ System.out.println("---get"); }
}class Mon extends Thread{
private Send send; private Get get; public Mon(){ super(); } public void run(){ while(true){ System.out.println("mon"); if(send.isAlive() == false){ send.start(); } if(get.isAlive() == false){ get.start(); } } } }
差不多了,给你改一下。 import java.lang.Thread; public class Test{ public static void main(String[] args){ Mon mon = new Mon(); mon.start(); } }class Send extends Thread{
public void run(){ System.out.println("---send"); } }class Get extends Thread{
public void run(){ System.out.println("---get"); }
}class Mon extends Thread{
private Send send; private Get get; public Mon(){ super(); } public void run(){ send = new Send(); // 要先初始化那个进程,否则下面调用isAlive()方法时会有空指针 get = new Get();
public class Test{
public static void main(String[] args){
Mon mon = new Mon();
mon.start();
}
}class Send extends Thread{
public void run(){
System.out.println("---send");
}
}class Get extends Thread{
public void run(){
System.out.println("---get");
}
}class Mon extends Thread{
private Send send;
private Get get;
public Mon(){
super();
}
public void run(){
while(true){
System.out.println("mon");
if(send.isAlive() == false){
send.start();
}
if(get.isAlive() == false){
get.start();
}
}
}
}
import java.lang.Thread;
public class Test{
public static void main(String[] args){
Mon mon = new Mon();
mon.start();
}
}class Send extends Thread{
public void run(){
System.out.println("---send");
}
}class Get extends Thread{
public void run(){
System.out.println("---get");
}
}class Mon extends Thread{
private Send send;
private Get get;
public Mon(){
super();
}
public void run(){
send = new Send(); // 要先初始化那个进程,否则下面调用isAlive()方法时会有空指针
get = new Get();
send.start(); //启动两个进程
get.start();
while(true){
if(send.isAlive() == false){
send = new Send();
send.start(); //java thread完成后,不能直接重启,要重新建立
}
if(get.isAlive() == false){
get = new Get();
get.start();
}
try {
Thread.sleep(1000); // 当thread停一下,把CPU时间留给其它线程。
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}