<分区>
我有以下主题:
public void start() {
isRunning = true;
if (mainThread == null) {
mainThread = new Thread(this);
mainThread.setPriority(Thread.MAX_PRIORITY);
}
if (!mainThread.isAlive()) {
try {
mainThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
有时我想停止它的运行:
public void stop() {
isRunning = false;
System.gc();
}
当再次调用 start()
时,会抛出以下异常:
java.lang.IllegalThreadStateException
指向 mainThread.start()
代码行。
启动/停止线程的最佳方式是什么?我怎样才能使这个线程可重用?
谢谢!