在完成之前调用“异步”

时间:2019-06-07 14:56:36

标签: spring spring-boot

我有以下设置,每隔X秒,它将调用Async方法,该方法将包含新项的列表传递给它。 Async具有自定义的Executor,如下所示

@Bean(name = "workExecutor")
public Executor getWorkAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(40);
    // executor.setMaxPoolSize(40);
    executor.setQueueCapacity(10000000);
    executor.setThreadNamePrefix("workExecutor-");
    executor.initialize();
    return executor;
}

以下类的方法每隔X秒/分钟执行一次(在这种情况下,每5秒执行一次),并将列表传递给run中的ThreadClass方法

@Component
public class ScheduledClass {
    @Autowired
    MyRepository myRepository;

    @Autowired
    ThreadClass threadClass;


    @Scheduled(cron= "*/5 * * * * *")
    public void callThreadClass() {
        try {
            List<String> list = myRepository.getList();

            for (int i = 0; i < myRepository.size(); i++) {
                threadClass.run(myRepository.get(i));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("---------------------");
        }
    }
}

完成所有工作的ThreadClass

@Component
public class ThreadClass {

    @Async("workExecutor")
    public void run(String str) {
        try {
            //do all the work
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

    }
}

如果run方法尚未完成先前的工作,并且callThreadClass方法执行并传递run新列表,将会发生什么?列表是否添加到队列中,取代了旧列表?

0 个答案:

没有答案
相关问题