我正在研究Quartz框架,我必须在10秒内触发这项工作,并且每个工作都会在10秒内完成工作。为了更加清晰,请看下面的代码。
public class CronTriggerExample
{
public static void main( String[] args ) throws Exception
{
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
我正在为我的测试条件运行无限循环。
public class HelloJob implements InterruptableJob
{
AtomicReference<Thread> runningThread = new AtomicReference<Thread>();
AtomicBoolean stopFlag = new AtomicBoolean(false);
static Date outcallExecuteJobRunTime = new Date();
static boolean prvJobRunning = false;
private static AtomicBoolean prvJobExecuted = new AtomicBoolean(true);
public void execute(JobExecutionContext context)
throws JobExecutionException {
long start = System.currentTimeMillis();
try{
System.out.println("["+Thread.currentThread().getName()+"] Running OutCallExecutor job");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, -20);
Date compareDate = cal.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
boolean isThreadExceededRunTime = compareDate.after(outcallExecuteJobRunTime);
System.out.println("compare date is : "+df.format(compareDate));
System.out.println("Running Thread is "+runningThread);
if(! isThreadExceededRunTime)
System.out.println("Previous job is running withing time limit");
if(!prvJobExecuted.get() ){
//Check if prev job is still running? If yes, then the thread is hanged/blocked -
//so interrupt it and set previous job to true
if(isThreadExceededRunTime){
System.out.println("Previouse job is running more the limit");
interrupt();
return;
}else{
System.out.println("Normal exit. Previous job is not executed yet");
return;
}
}
prvJobExecuted.set(false);
this.runningThread.set(Thread.currentThread());
System.out.println("Running Thread is "+runningThread.get().getName());
outcallExecuteJobRunTime = new Date();
System.out.println("OutCallExcecutor :: outcallExecuteJobRunTime : "+outcallExecuteJobRunTime);
System.out.println("Running OutCallExecutor executed.");
while(1 == 1){}
}catch(Exception e){
e.printStackTrace();
System.out.println("OutCallExecutor :: Exception occured while OutCallExecutor. Exception "+e.getMessage());
prvJobExecuted.set(true);
}
prvJobExecuted.set(true);
}
@Override
public void interrupt() throws UnableToInterruptJobException {
//System.out.println("HelloJob.interrupted BZZZZZZZZZZ(). Stopping running Thread "+runningThread.get().getName());
System.out.println("===1=====");
prvJobExecuted.set(true);
System.out.println("===2=====");
Thread thread = runningThread.getAndSet(null);
System.out.println("===1===== Thread is "+thread);
if (thread != null){
if(thread.getName() != null){
System.out.println("===1===== Thread Name is "+thread.getName());
}
thread.interrupt();
}
}
}
只是为了理解我正在打印程序的输出
[DefaultQuartzScheduler_Worker-1] Running OutCallExecutor job
compare date is : 2016-07-21 23:01:00
Running Thread is null
Previous job is running withing time limit
Running Thread is DefaultQuartzScheduler_Worker-1
OutCallExcecutor :: outcallExecuteJobRunTime : Thu Jul 21 23:01:20 IST 2016
Running OutCallExecutor executed.
[DefaultQuartzScheduler_Worker-2] Running OutCallExecutor job
OutCallExecutor compare date is : 2016-07-21 23:01:10
Running Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
Previous job is running withing time limit
Normal exit. Previous job is not executed yet
======================================================
====================================================
[DefaultQuartzScheduler_Worker-3] Running OutCallExecutor job
compare date is : 2016-07-21 23:01:20
Running Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
Previous job is running withing time limit
Normal exit. Previous job is not executed yet
======================================================
====================================================
[DefaultQuartzScheduler_Worker-4] Running OutCallExecutor job
compare date is : 2016-07-21 23:01:30
Running Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
Previouse job is running more the limit
===1=====
===2=====
===1===== Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
===1===== Thread Name is DefaultQuartzScheduler_Worker-1
当我中断WorkerThread-1然后它再次没有分配给线程池并且它丢失了,而我想要线程池默认大小应该保持相同。 任何人都可以帮我杀死或停止线程池大小不会影响的线程,如上面编写的程序我松散整个线程池和最后一个线程永远不会中断,因为新的工作不会打到服务。
先谢谢!!
答案 0 :(得分:0)
您的线程似乎没有检查它是否被中断。尝试
while (!Thread.currentThread().isInterrupted())
而不是
while(1 == 1)