2

可能的重复:
终止的线程复兴

 Thread threadWait = new Thread()
    {
        @Override
        public void run() {
            try {
                sleep(10000);
                sync = false;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }               
    };

我在这里调用这个线程:

            threadWait.start();
        while(sync){
        //do something
        }

当 threadWait 完成时,状态为 TERMINATED。我如何才能再次启动线程?任何想法?谢谢大家!

解决方案:

Runnable runWait = new Runnable(){
    public void run(){
        try {
            Thread.sleep(10000);
            sync = false;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
};

和..

Thread first = new Thread(runWait);
            first.start();
4

2 回答 2

1

您不能Thread多次启动同一个对象。只需新建Thread并启动它。

于 2012-10-08T16:51:57.620 回答
1

我如何才能再次启动线程?

你不能。任何线程只能运行一次。当然,您可以根据需要创建一个新Thread线程Runnable,然后启动第二个线程......

Runnable runnable = new Runnable() {
    // Code as before
};
Thread first = new Thread(runnable);
first.start();
... Maybe the first thread dies...
Thread second = new Thread(runnable);
second.start();
于 2012-10-08T16:52:15.257 回答