這是我使用線程的一個小程序片段。線程不停留活 - IllegalStateException
JOptionPane.showMessageDialog(null, "Before: " + thread.isAlive());
if (!thread.isAlive()) {
JOptionPane.showMessageDialog(null, "Thread is not alive.");
thread.start();
}
JOptionPane.showMessageDialog(null, "After: " + thread.isAlive());
此代碼使用按鈕激活。當我第一次按下按鈕時,我正確地得到「Before:false」,然後「After:true」。 當我再次按下按鈕時,我錯誤地得到「Before:false」,然後「After:true」,但是期望Before:true,因爲我沒有銷燬線程或覆蓋變量。
我認爲,這是什麼原因造成這IllegalStateException異常我得到(糾正我,如果我錯了,對了!)
任何人都可以向我解釋什麼,我做錯了什麼?
編輯:
public class SomeClass extends Applet
{
private ClassThatExtendsThread thread;
public void init()
{
super.init();
//Some UI elements are created here.
thread = new ClassThatExtendsThread (/*there are some parameters*/);
}
你在哪裏以及如何開始'thread'? – NPE
*我相信這是什麼導致我得到的IllegalStateException *:什麼是* this *?什麼是異常的完整堆棧跟蹤?我寧願說這是你得到異常的結果*,這可能導致線程死亡。 –
我已經添加了線程類如何存儲和實例化 –