多线程问题:
如何判断main线程进入dead状态?代码如下:package thread;public class MyThread001 extends Thread {
public void run() {
for (int a = 0; a < 50; a++) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + " " + a);
}
} public static void main(String args[]) {
MyThread001 t1 = new MyThread001();
MyThread001 t2 = new MyThread001();
t1.start();
t2.start();
/**
 * 执行到这里,main线程是否进入dead状态?
 * 
 * 能否通过代码进行判断?
 * 怎么判断?
 */
}
}

解决方案 »

  1.   

    可否设置一个deamon线程,作为标识状态
      

  2.   


    能否详细 说明 如何 设置一个deamon线程?
    请赐教!!
      

  3.   

    你要在main方法里判断main线程的状态
    可谓 不是庐山真面目,只缘身在此山中阿所以,需要启动其他线程来监视main线程的状态
      

  4.   


    如果main线程死了
    就剩下这个后台线程了的话,这个后台线程是会退出的哦
      

  5.   


    在其他线程中监视main线程,可行
    但是mian线程的引用是什么呢??
      

  6.   

    注释那里加上  System.out.println(Thread.activeCount()); 试试
      

  7.   


    public class Test7 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("This is main thread");
    Watch_Main wm = new Watch_Main(Thread.currentThread());
    wm.start();

                   //随便写一个操作,为了让main多活会,呵呵
    int temp;
    for(int i=0; i<1000000000;i++){
    temp = i;
    }
    }
    }class Watch_Main extends Thread{
    public Thread test_main;
    Watch_Main(Thread test){
    test_main = test;
    }

    public void run(){

    while(true){
    if(test_main.getState()==Thread.State.TERMINATED){
    System.out.println("main thread has already dead!");
    break;
    }else{
    System.out.println("watch the main thread");
    try{
    sleep(2000);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    }
    }