简单写了一下,不见得是很好的解决方法。使用main线程和TestRunnable线程wait/notify来协作。每次TestRunnable执行完成就等待,由其它线程通知它再次启动。
这个方法并不是重启动线程(线程就没有死掉过:) )是一种绕过去的方法,可能和你的需求不符合。不过再加修改也不是难事。
<<
public class TestRunnable implements Runnable {
    String str;
    private boolean pause = false;
    public boolean isPause() {return pause;}    public TestRunnable(String s) {
        str = s;
    }    public void run() {
        while (!pause) {
            try {
                System.out.println(str);
                Thread.sleep(10000);
            } catch (Exception e) {
            }            synchronized(this) {
                this.notifyAll();
                try{this.wait();}catch(InterruptedException ex){}
            }
        }
    }    public static void main(String args[]) {
        TestRunnable t1 = new TestRunnable("test...1 ");
        Thread r1 = new Thread(t1);
        r1.start();        synchronized(t1) {
            while(!t1.isPause()) {
                try{t1.wait();}catch(InterruptedException ex){}
                t1.notifyAll();
            }
        }
    }
}
>>

解决方案 »

  1.   

    好像有个Thread.currentThread().isAlive()
    若这样
    if (Thread.currentThread().isAlive())
    {
      myThread = true;
    }
    else
    {
    myThread = false;} 
    在另一线程中如何判断myThread 的值,若能判断,是不是就知道这个线程是否已死了。还有以上那段IF代码是写在另一线程中吗?
      

  2.   

    楼主可能没搞清楚概念,什么叫“线程死掉了”?
    线程死掉了,就是线程的run方法以某种方式结束掉了;你写的那个线程的run方法根本就永远不会自动退出,除非你在主线程中将它干掉(如:stop()),但是,这样做是不合理的,sun depricate了Thread类那么多方法,目的就是让正确的处理线程的退出;而且,如果是你自己stop了一个线程,那么你知道什么时候它死掉了。如果它确实以某种你不知道的方式死掉了,那一定是不可控的,譬如整个进程被kill掉了;线程死了,线程对象还可以重用(没有线程重用的说法呵),只要线程对象的句柄还被某个对象持有,线程对象就不会被垃圾收集;可以调用线程对象的isAlive方法来判断一个线程对象上有没有线程正在运行;你说的可能就是这个意思了,可以在主线程中每隔一段时间测试一下:
     public static void main(String args[])
        {
            testRunnable t1=new testRunnable("test...1   ");
            Runnable t1=new testRunnable("test....1");
            Thread r1 =new Thread(t1);
            r1.start(); while(true)
    {
    if (!r1.isAlive())
    r1.start();
    Thread.currentThread().sleep(1000);
    }    }
      

  3.   

    一个线程对象只能被start一次。