Я пытаюсь выполнить планирование работы. В то время как планировщик сам по себе работает нормально, я хотел использовать синхронизированный блок, чтобы одновременно не запускать два задания (так как будет доступ к БД проблемы параллелизма). Но, к сожалению, работа ожидания не просыпаясь на извещатьВвод нити Синхронизированный блок при использовании с планировщиком
Ниже приведен код, используемый: для работы слушателя
public class SJobListener implements JobListener {
public static final String LISTENER_NAME = "SJobListener";
ExecutingClass ec = 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 (ec) {
System.out.println(Thread.currentThread().getName());
System.out.println("SYNCHRONIZED BLOCK"+jobName);
if(!condition){
try {
System.out.println("Going to Wait");
Thread.currentThread().wait(200);
//check the condition again
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//ec .execute(context); as scheduler automatically calls this method, explicitly calling it runs the method twice , one by scheduler and the other executed explicitly
}
//}
//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());
/*String testCaseName = context.getJobDetail().getDescription();
QueryBuilderUtil.updateRecordInSchedular(testCaseName,"completed");*/
if (!jobException.getMessage().equals("")) {
System.out.println("Exception thrown by: " + jobName
+ " Exception: " + jobException.getMessage());
jobException.printStackTrace();
}
Thread.currentThread().notifyAll();
}
Ниже приведен код для ExecutingClass
public class ExecutingClass implements Job{
private MyClass mc = new MyClass();
public synchronized void execute(JobExecutionContext context) {
try
{
System.out.println("--------execute------ "+context.getJobDetail().getKey());
System.out.println("--------Current Thread------ "+Thread.currentThread().getName()+" "+Thread.currentThread().getId());
mc .executeMethod();
System.out.println("Done MyClass.executeTestCase");
}
catch (BusinessException e) {
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
Ниже приведен код MyClass
public class MyClass(){
public synchronized void executeMethod(){
//does the actual processing i.e read write from DB
}
}
Проблема в том, что потоки, идущие позже, идут ждать, но не являются FY не делает работу run..or возможно уведомлять не работает .. Я не know..something wrong..the ожидающего нить никогда не делает свою работу .. Благодаря
Вы выполняете метод 'execute', синхронизированный в экземпляре, и' jobToBeExecuted' на классе. Он предназначен? – talex
извините, не получил запрос – user2176576