Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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 使用ScheduledTaskRegistrar在多线程上划分任务_Java_Multithreading_Spring Boot_Scheduled Tasks_Threadpool - Fatal编程技术网

Java 使用ScheduledTaskRegistrar在多线程上划分任务

Java 使用ScheduledTaskRegistrar在多线程上划分任务,java,multithreading,spring-boot,scheduled-tasks,threadpool,Java,Multithreading,Spring Boot,Scheduled Tasks,Threadpool,我有一个从数据库加载的对象提要列表,然后我使用这些对象通过属性UrlFeed访问internet,并做一些工作从每个提要获取项目,问题是我使用一个线程,输出时间取决于列表的大小,所以如果列表变大,输出时间将增加。我想使用多个线程并减少此输出时间。我想知道如何将此任务划分为多个线程,或者如何减少输出时间 我使用Spring Boot并使用ScheduledTaskRegistrar定期安排任务 这是我的配置类: @SpringBootApplication @EnableScheduling pu

我有一个从数据库加载的对象提要列表,然后我使用这些对象通过属性UrlFeed访问internet,并做一些工作从每个提要获取项目,问题是我使用一个线程,输出时间取决于列表的大小,所以如果列表变大,输出时间将增加。我想使用多个线程并减少此输出时间。我想知道如何将此任务划分为多个线程,或者如何减少输出时间

我使用Spring Boot并使用ScheduledTaskRegistrar定期安排任务 这是我的配置类:

@SpringBootApplication
@EnableScheduling
public class MyApp implements SchedulingConfigurer {

public static void main(String[] args) {
    SpringApplication.run(MyApp.class, args);
}

@AutoWired
TaskConfig tC;


@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
    return Executors.newScheduledThreadPool(20);
}

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

    taskRegistrar.setScheduler(taskExecutor());

    taskRegistrar.addTriggerTask(
            new Runnable() {
                @Override 
                public void run() {

                    for(Feed f : getListFeed()){

                          accessToFeedAndDoSomeWork(f.getUrlFeed);
                    }


                    }
                }, 
            new Trigger() {

                    @Override
                    public Date nextExecutionTime(TriggerContext triggerContext) {
                        Calendar nextExecutionTime =  new GregorianCalendar();
                        Date lastActualExecutionTime = triggerContext.lastActualExecutionTime();
                        nextExecutionTime.setTime(lastActualExecutionTime != null ? lastActualExecutionTime : new Date());
                        nextExecutionTime.add(Calendar.MILLISECOND, tC.getRate);
                        return nextExecutionTime.getTime();
                    }
                });



}
}

ScheduledTaskRegistrar是否有解决方案,谢谢您的帮助

您是否尝试过按url创建一个任务,而不是一个在url上迭代的任务?如果这样做,我将以大量任务结束,因为这取决于url的数量,所以200个url将生成200个任务,我认为这是一个lotno,你的线程池是有限制的:你将有200个任务,但只有20个线程(代码中有20个)啊,好吧,现在我明白了,所以剩下的任务将等待轮到它们执行,我是对的?如果我想停止任务,现在感谢jérémie,如果我将空值传递给ionTime.add(Calendar.millis秒,tC.getRate);