这是我的班级。我正在使用一个Quartz调度器,并且一旦一个工作被执行,我想避免并发......使用Synchronize关键字..并使用等待每个线程,但似乎一旦工作执行..通知不会调用等待线程..请帮助...从最后两天卡在这个:通知未醒来等待线程
public class SJobListener implements JobListener {
public static final String LISTENER_NAME = "SchedulerJobListener";
ExecutingClass compSched = new ExecutingClass();
@Override
public String getName() {
return LISTENER_NAME; //must return a name
}
// Run this if job is about to be executed.
@Override
public void jobToBeExecuted(JobExecutionContext context) {
String jobName = context.getJobDetail().getKey().toString();
System.out.println("jobToBeExecuted");
System.out.println("Listener : Job : " + jobName + " is going to start...");
System.out.println("Thread running in jobToBeExecuted :"+Thread.currentThread().getName()+" "+Thread.currentThread().getId());
synchronized (compSched) {
if(!condition)
try {
System.out.println("Going to Wait");
Thread.currentThread().wait(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//Run this after job has been executed
@Override
public void jobWasExecuted(JobExecutionContext context,
JobExecutionException jobException) {
System.out.println("jobWasExecuted");
String jobName = context.getJobDetail().getKey().toString();
System.out.println("Listener :Job : " + jobName + " is finished...");
System.out.println("Thread running in jobWasExecuted :"+Thread.currentThread().getName()+" "+Thread.currentThread().getId());
//synchronized (compSched) {
System.out.println("Notifying waiting threads");
//context.notifyAll();
Thread.currentThread().notifyAll();
if (!jobException.getMessage().equals("")) {
System.out.println("Exception thrown by: " + jobName
+ " Exception: " + jobException.getMessage());
jobException.printStackTrace();
}
System.out.println("Out Of jobWasExecuted");
}
}
在此先感谢。
您目前的实现将抛出IllegalStateException异常请张贴实际的代码。 – talex 2014-09-26 09:37:59
从理论上讲,在线程对象中调用wait和notiy并不是一个好习惯。如果任何线程正在调用join,那么它可能会受到影响 – 2014-09-26 09:39:32
[调用notify不会唤醒另一个等待线程](http: //stackoverflow.com/questions/20440485/calling-notify-is-not-waking-up-the-other-waiting-thread) – Raedwald 2014-09-26 10:24:20