通知不要等待等待的线程

时间:2014-09-26 09:34:09

标签: java multithreading quartz-scheduler synchronized

这是我的班级。我正在使用Quartz调度程序,并且一旦作业被执行,我想避免并发..因此使用了Synchronize关键字..并使用等待每个线程但似乎一旦作业执行..Notify不会调用等待线程..请帮助......最近两天坚持这个:

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");
    }


}

提前致谢。

3 个答案:

答案 0 :(得分: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
   }
  }
}

答案 1 :(得分:0)

Thread.currentThread().wait(200);替换为compSched.wait(200)

在jobWasExecuted中,您应该致电notify上的compSched

答案 2 :(得分:0)

方法jobToBeExecuted和jobWasExecuted正在不同的线程中运行,因此您正在等待另一个对象并期望在另一个对象上发送通知。这就是为什么它不起作用。

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