我想终止石英调度程序中的某些多线程

时间:2019-06-25 02:17:48

标签: java quartz-scheduler

我在Java中使用石英调度程序。

我想构建一个多线程环境,其中多个调度程序可以同时运行。

动态地,必须添加或删除线程。(不重新启动应用程序)

================================================ ====================== Main.java

Scheduler schduler = new StdSchedulerFactory().getScheduler();

    List<SimpleTriggerImpl> triggerList = new ArrayList<SimpleTriggerImpl>();

    List<JobDetail> jobDetailList = new ArrayList<JobDetail>();

    jobDetailList.add(JobBuilder.newJob(JobTest1.class).build());
    jobDetailList.add(JobBuilder.newJob(JobTest2.class).build());

    for (int i = 0; i < 2; i++) {
        triggerList.add((SimpleTriggerImpl) TriggerBuilder.newTrigger()
                .withIdentity(String.valueOf(i), Scheduler.DEFAULT_GROUP).startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withMisfireHandlingInstructionNowWithExistingCount().withIntervalInMilliseconds(10000) 
                        .withRepeatCount(SimpleTriggerImpl.REPEAT_INDEFINITELY))
                .build());
    }

    schduler.scheduleJob(jobDetailList.get(0), triggerList.get(0));
    schduler.scheduleJob(jobDetailList.get(1), triggerList.get(1));
    schduler.start();

JobTest1.java

public class JobTest1 implements Job{

public void execute(JobExecutionContext context) throws JobExecutionException {
    for(int i = 0; i<10;i++) {
        System.out.println("this is JobTest1");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

JabTest2.java

public class JobTest2 implements Job{

public void execute(JobExecutionContext context) throws JobExecutionException {

    for(int i = 0; i<10;i++) {

        System.out.println("this is JobTest2");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

一个问题

  1. 如何在多线程环境中终止某些特定线程

  2. 如何在实现作业的execute()方法中检查当前线程间隔

  3. 如何在实现作业的execute()方法中添加新线程

0 个答案:

没有答案