下边是我写的一个  测试线程   :public class TestThread {
private Thread thread1;
 
public static void main(String[] args) {
TestThread t = new TestThread();
t.createTread1();
}
/*
 * 启动线程
 */
public void createTread1(){
thread1 = new Thread(new ThreadI());
thread1.start();
}
/*
 * 判断线程是否死亡,重启线程
 */
public void resetThread1(){
     System.out.println("是否存活: "+thread1.isAlive());
     if(!thread1.isAlive())
     {
     createTread1();
     }
    }
/*
 * 伺服线程
 */
class ThreadI implements Runnable {
        private int temp = 0;
public void run() {
try{
while (true) {
try {
if(temp>=10)
throw new RuntimeException();
System.out.println("------------循环显示中---------");
Thread.sleep(2000);
temp++;
} catch (Exception e) {
break;
}
}
}finally{
System.out.println("后判断 是否存活");
//try {
// Thread.currentThread().join();
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
resetThread1();
}

} }
}
以上程序 是我初步的设想 但是这样不能达到我的要求  因为当在finally块中调用 resetThread1方法是 线程并不认为run()方法已经结束,也就是没有死亡.我想到了另外一个方法,就是不在本线程中 设置重启,另起一个伺服线程 每隔 一段时间 判断本线程是否存活,这样可以实现,但是 一起启动两个伺服线程 我认为很耗内存 ,不知各位高人 有没有好的解决办法 启动一个伺服线程就能解决 意外死亡自动重启 的问题。先谢过了

解决方案 »

  1.   


    public class TestThread {
        private Thread thread1;
        boolean flag = false;//加个标志位    public static void main(String[] args) {
            TestThread t = new TestThread();
            t.createTread1();
        }
        /*
         * 启动线程
         */
        public void createTread1(){
            thread1 = new Thread(new ThreadI());
            thread1.start();
        }
        /*
         * 判断线程是否死亡,重启线程
         */
        public void resetThread1(){
            System.out.println("是否存活: "+thread1.isAlive());
            if(flag) //用标志位判断
            {
                createTread1();
                falg = false; //创建好后再置回false;以便下次再用
            }
        }
        /*
         * 伺服线程
         */
        class ThreadI implements Runnable {
            private int temp = 0;
            public void run() {
                try{
                    while (true) {
                        try {
                            if(temp>=10)
                            throw new RuntimeException();
                            System.out.println("------------循环显示中---------");
                            Thread.sleep(2000);
                            temp++;
                        } catch (Exception e) {
                            break;
                        }
                    }
                }finally{
                    System.out.println("后判断 是否存活");
                    flag = true;// 结束之前把标志位置true
                    //try {
                    //    Thread.currentThread().join();
                    //} catch (InterruptedException e) {
                    //    e.printStackTrace();
                    //}
                    resetThread1();
                }
            
            }    }
    }
      

  2.   

    这么说吧  如果 按着这种要求 我连状态位都不用  直接调用
        public void resetThread1(){
                createTread1();
        }
    就ok了被
      

  3.   


    private Thread thread1;
     
    public static void main(String[] args) {
    TestThread t = new TestThread();
    t.createTread1();
    }
    /*
     * 启动线程
     */
    public void createTread1(){
    thread1 = new Thread(new ThreadI());
    thread1.start();
    }
    /*
     * 判断线程是否死亡,重启线程
     */
    public void resetThread1(){
    thread1=null;
         createTread1();
        }
    /*
     * 伺服线程
     */
    class ThreadI implements Runnable {
            private int temp = 0;
    public void run() {
    try{
    while (true) {
    try {
    if(temp>=10)
    throw new RuntimeException();
    System.out.println("------------循环显示中---------");
    Thread.sleep(2000);
    temp++;
    } catch (Exception e) {
    break;
    }
    }
    }finally{
    System.out.println("后判断 是否存活");
    resetThread1();
    }

    } }
    }改成这样了 虽然用 stop() destroy() null 都不是推荐方法  但是我么办法了
      

  4.   

    try{
    ....
    }
    catch(Throwable ex){
       resetThread1();
    }