這是我的班級。我正在使用一個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