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