2014-09-26 132 views
0

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


} 

在此先感謝。

+0

您目前的實現將拋出IllegalStateException異常請張貼實際的代碼。 – talex 2014-09-26 09:37:59

+0

從理論上講,在線程對象中調用wait和notiy並不是一個好習慣。如果任何線程正在調用join,那麼它可能會受到影響 – 2014-09-26 09:39:32

+0

[調用notify不會喚醒另一個等待線程](http: //stackoverflow.com/questions/20440485/calling-notify-is-not-waking-up-the-other-waiting-thread) – Raedwald 2014-09-26 10:24:20

回答

2

請閱讀java併發性:

線程等待鎖定。這個鎖用於通知等待同一個鎖的其他線程。

考慮:

public class SynchronizedExample{ 

    private final Object LOCK = new Object(); 

    public void doSomethingOr() { 
    if(somethingIsNotDone()) { 
     synchronize(LOCK) { 
     LOCK.wait(); //trycatch here 
     } 
    } 
    } 

    public void somethingSone() { 
    somethingIsDone = true; 
    synchronized(LOCK) { 
    LOCK.notifyAll(); //trycatch 
    } 
    } 
} 
0

Thread.currentThread().wait(200);替換爲compSched.wait(200)

而且在jobWasExecuted你應該叫notifycompSched

0

的方法jobToBeExecuted和jobWasExecuted在不同的線程中運行,所以你在等待一個不同的對象和不同的對象上預期的通知。這就是爲什麼它不起作用。

如果您更簡潔地解釋了您的需求,可以提供不同於等待通知機制的解決方案。