2014-09-26 128 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在不同的线程中运行,所以你在等待一个不同的对象和不同的对象上预期的通知。这就是为什么它不起作用。

如果您更简洁地解释了您的需求,可以提供不同于等待通知机制的解决方案。