我的多线程应用程序有一个创建多个线程的主类。主类将在启动一些线程后等待。我创建的可运行类将获得文件列表,获取文件以及通过调用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();
我之所以做了观望,因为我不想让主类运行,除非有必要通知过程创建另一个线程。
主要类的目的是产生线程。这个类有一个无限循环来永远创建和完成线程。
无限循环的目的是不断更新公司名单。
您是否添加了任何类型的日志以了解发生了什么? –
请注意,只有在有线程在等待的情况下,通知才有效。如果您的主线程还没有完全等待,通知将落在地面上。 –
你可能会用信号量做得更好。 –