Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的多线程任务调度器_Java_Multithreading_Concurrency_Task_Scheduler - Fatal编程技术网

java中的多线程任务调度器

java中的多线程任务调度器,java,multithreading,concurrency,task,scheduler,Java,Multithreading,Concurrency,Task,Scheduler,我有一些任务,我们需要在每个任务定义的时间间隔内一次又一次地执行,每个任务都必须在各自不同的线程中执行。 示例:假设我有task1、task2、task3、task4……taskN 我想在规定的时间间隔内反复执行每个任务 e、 g 每1秒后的task1间隔 任务2,4秒 任务3,2秒 10秒内完成任务 对于每个任务,应该创建不同的线程,而不是for循环 请建议该方法并共享示例java代码,或者假设我有10个数据库查询,所有查询都必须在不同的线程中运行,并在定义的时间间隔后一次又一次地运行,

我有一些任务,我们需要在每个任务定义的时间间隔内一次又一次地执行,每个任务都必须在各自不同的线程中执行。

示例:假设我有task1、task2、task3、task4……taskN

我想在规定的时间间隔内反复执行每个任务
e、 g

  • 每1秒后的task1间隔
  • 任务2,4秒
  • 任务3,2秒
  • 10秒内完成任务
对于每个任务,应该创建不同的线程,而不是for循环


请建议该方法并共享示例java代码,或者假设我有10个数据库查询,所有查询都必须在不同的线程中运行,并在定义的时间间隔后一次又一次地运行,直到无限。

对于我建议的调度。 您可以设置将在自己的线程上运行的任务

例如,创建StatefulJob实现:

class RunQueryJob implements StatefulJob {
    public void execute(JobExecutionContext jec) throws JobExecutionException {
        try {
            String query = (String) jec.getMergedJobDataMap().get("query");
            //run query
        } catch (Exception ex) {
            //Handle Exception
        }
    }
}
初始化计划程序:

Scheduler scheduler;
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
创建用于计划的方法:

public void scheduleQuery( String query, int seconds ){
   JobDataMap map =new JobDataMap();
   map.put("query", query);

   JobDetail job = JobBuilder.newJob(RunQueryJob.class)
                    .usingJobData( map ).
                    build();

   Trigger trigger = TriggerBuilder
        .newTrigger()
        .withSchedule(
            CronScheduleBuilder.cronSchedule("0/"+seconds+" * * * * ?"))
        .build();

   scheduler.scheduleJob(job, trigger);

}
请注意
CronScheduleBuilder.cronSchedule(“0/”+秒+“****?”)
部分。你可以有更多的信息

之后,您可以使用以下方法:


scheduleQuery(“QRY1”,3)//RunQueryJob类将每隔3秒在新线程中运行一次,并在数据映射中传递“QRY1”。

对于我建议的调度。 您可以设置将在自己的线程上运行的任务

例如,创建StatefulJob实现:

class RunQueryJob implements StatefulJob {
    public void execute(JobExecutionContext jec) throws JobExecutionException {
        try {
            String query = (String) jec.getMergedJobDataMap().get("query");
            //run query
        } catch (Exception ex) {
            //Handle Exception
        }
    }
}
初始化计划程序:

Scheduler scheduler;
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
创建用于计划的方法:

public void scheduleQuery( String query, int seconds ){
   JobDataMap map =new JobDataMap();
   map.put("query", query);

   JobDetail job = JobBuilder.newJob(RunQueryJob.class)
                    .usingJobData( map ).
                    build();

   Trigger trigger = TriggerBuilder
        .newTrigger()
        .withSchedule(
            CronScheduleBuilder.cronSchedule("0/"+seconds+" * * * * ?"))
        .build();

   scheduler.scheduleJob(job, trigger);

}
请注意
CronScheduleBuilder.cronSchedule(“0/”+秒+“****?”)
部分。你可以有更多的信息

之后,您可以使用以下方法:


scheduleQuery(“QRY1”,3)//RunQueryJob类将每隔3秒在新线程中运行一次,并在数据映射中传递“QRY1”。

您可以使用java.util.concurrent提供的Executor服务


可能会有帮助。

您可以使用java.util.concurrent提供的Executor服务


可能会有帮助。

一个简单的解决方案是利用Java,并使用
scheduleAtFixedRate
方法

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                          long initialDelay,
                                          long delay,
                                          TimeUnit unit)

Creates and executes a periodic action that becomes enabled first after the given initial
delay, and subsequently with the given delay between the termination of one execution
and the commencement of the next. If any execution of the task encounters an exception, 
subsequent executions are suppressed. Otherwise, the task will only terminate
via cancellation or termination of the executor.

Parameters:
  command - the task to execute
  initialDelay - the time to delay first execution
  delay - the delay between the termination of one execution 
          and the commencement of the next
  unit - the time unit of the initialDelay and delay parameters
输出示例(用于1次运行)。您的结果应该相似,但可能不完全相同。每个任务首先在所提供的
单元中的
初始延迟后执行,然后计划在每个
延迟后执行:

T3: 38
T4: 2039
T1: 3039
T4: 4040
T2: 5040
T3: 5040
T1: 6039
T4: 6040
T2: 8040
T4: 8040
T1: 9039
T3: 10040
T4: 10040
T2: 11040
T1: 12039
T4: 12040
T2: 14039
T4: 14039

一个简单的解决方案是利用Java并使用
scheduleAtFixedRate
方法

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                          long initialDelay,
                                          long delay,
                                          TimeUnit unit)

Creates and executes a periodic action that becomes enabled first after the given initial
delay, and subsequently with the given delay between the termination of one execution
and the commencement of the next. If any execution of the task encounters an exception, 
subsequent executions are suppressed. Otherwise, the task will only terminate
via cancellation or termination of the executor.

Parameters:
  command - the task to execute
  initialDelay - the time to delay first execution
  delay - the delay between the termination of one execution 
          and the commencement of the next
  unit - the time unit of the initialDelay and delay parameters
输出示例(用于1次运行)。您的结果应该相似,但可能不完全相同。每个任务首先在所提供的
单元中的
初始延迟后执行,然后计划在每个
延迟后执行:

T3: 38
T4: 2039
T1: 3039
T4: 4040
T2: 5040
T3: 5040
T1: 6039
T4: 6040
T2: 8040
T4: 8040
T1: 9039
T3: 10040
T4: 10040
T2: 11040
T1: 12039
T4: 12040
T2: 14039
T4: 14039

这可能会帮助您嗨,我不想使用间隔,需要在定义的时间内执行任务,比如说在11:30。这可能会帮助您嗨,我不想使用间隔,需要在定义的时间内执行任务,比如说在11:30。这不会获得每个任务的独立线程。基于任何可用线程,如果任何任务都应该被执行,它将被一个随机空闲线程执行。这不会为每个任务获得独立的线程。基于任何可用线程,如果任何任务都应该执行,它将被一个随机空闲线程执行。