【发布时间】:2013-03-21 12:50:51
【问题描述】:
我正在编写一个程序,我需要一个线程停止并启动另一个线程。我的问题是,如果我 t1.stop() 比 t1.start() 我得到 java.lang.IllegalThreadStateException
if (t1.isAlive() == true){
t1.stop();
// above I stop the thread and call another
t2.start();
System.out.println("t1 was playing");
}else{
t2.stop();
t1.start();
// above I stop the other thread and want to start the first thread again, but when I run the program I get the exception i said above
}
【问题讨论】:
-
这不是问题。
-
-1 搜索首先。我用了
IllegalThreadStateException thread start,发现不缺重复项。 -
(在任何情况下,解决方案是创建一个 new 线程或提供一种机制来“空闲”线程,当它需要暂时挂起 - 例如当一个游戏暂停。)
标签: java multithreading