我的多線程應用程序有一個創建多個線程的主類。主類將在啓動一些線程後等待。我創建的可運行類將獲得文件列表,獲取文件以及通過調用Web服務來刪除文件。線程完成後,它會通知主類再次運行。我的問題是它的工作了一段時間,但可能在一個小時左右後,它會從我在日誌中看到的輸出到達run方法的底部,就是這樣。 Java進程仍在運行,但根據我在日誌中查看的內容,它不會執行任何操作。爲什麼我的多線程應用程序暫停了?
主要類方法:
主要方法
while (true) {
// Removed the code here, it was just calling a web service to get a list of companies
// Removed code here was creating the threads and calling the start method for threads
mainClassInstance.waitMainClass();
}
public final synchronized void waitMainClass() throws Exception {
// synchronized (this) {
this.wait();
// }
}
public final synchronized void notifyMainClass() throws Exception {
// synchronized (this) {
this.notify();
// }
}
本來我實例上的同步,但它改變的方法。 Web服務日誌或客戶端日誌中也沒有記錄錯誤。我的假設是我做了等待,並通知錯誤或我錯過了一些信息。
運行的線程代碼:
在run方法結束
// This is a class member variable in the runnable thread class
mainClassInstance.notifyMainClass();
我之所以做了觀望,因爲我不想讓主類運行,除非有必要通知過程創建另一個線程。
主要類的目的是產生線程。這個類有一個無限循環來永遠創建和完成線程。
無限循環的目的是不斷更新公司名單。
您是否添加了任何類型的日誌以瞭解發生了什麼? –
請注意,只有在有線程在等待的情況下,通知纔有效。如果您的主線程還沒有完全等待,通知將落在地面上。 –
你可能會用信號量做得更好。 –