运行终止线程java android

时间:2012-10-08 16:49:36

标签: java android multithreading

  

可能重复:
  Terminated Thread Revival

 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。我怎么能再次启动线程?任何的想法? THX TO ALL!

解决方案:

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();

2 个答案:

答案 0 :(得分:1)

您无法多次启动相同的Thread对象。只需创建新的Thread并启动它。

答案 1 :(得分: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();