我正在开发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);
}
}
职务类别
对于我的测试条件,我正在运行无限while循环。
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时,它不再分配给线程池,它丢失了,而我希望线程池的默认大小应该保持不变。有人能帮我杀死或停止线程池大小不会影响的线程吗?在上面写的程序中,我失去了整个线程池,最后一个线程永远不会中断,因为新作业不会影响服务。
提前感谢!!
您的线程不会出现来检查它是否被中断。尝试
while (!Thread.currentThread().isInterrupted())
而不是
while(1 == 1)