0
我想通過3個不同的線程重複調用方法。但經過我我循環的下一次迭代中開始我的線程,他們都因此終止執行什麼...... 的代碼如下:爲什麼我的線程被終止?
public static void main(String[] args) {
main = new Main();
pollingThread.start();
}
static Thread pollingThread = new Thread() {
@Override
public void run() {
while (isRunning) {
main.poll();
// test the state of the threads
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
};
public void poll() {
if (clientThread == null) {
clientThread = new Thread(new Runnable() {
@Override
public void run() {
//create some objects
}
});
clientThread.start();
}
else if (clientThread.isAlive()) {
// do some treatment
}
if (gestionnaireThread == null) {
gestionnaireThread = new Thread(new Runnable() {
@Override
public void run() {
//create some objects
};
});
gestionnaireThread.start();
}
else if (gestionnaireThread.isAlive()) {
// do some treatment
}
if (marchandThread == null) {
marchandThread = new Thread(new Runnable() {
@Override
public void run() {
// create some objects
};
});
marchandThread.start();
}
else if (marchandThread.isAlive()) {
// do some treatment
}
}
出於某種原因,當我測試狀態我的不同線程,他們顯示爲可運行,然後第二次迭代,他們都被終止... 我做錯了什麼? 我其實沒有錯誤,但線程終止,所以我的循環不斷循環,並告訴我線程終止....
事實上,這確實有很大幫助。現在我有其他問題,但沒有線程了。感謝你及時的答覆。 – Sephy