首页新闻找找看学习计划

triggerTask中的线程与task如何传值?

0
悬赏园豆:50 [待解决问题]

动态添加定时任务:
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {

private ScheduledTaskRegistrar taskRegistrar;

private Map<String, ScheduledFuture<?>> taskFutures = new ConcurrentHashMap<String, ScheduledFuture<?>>();

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    this.taskRegistrar = taskRegistrar;
}

public void addTriggerTask(String taskId, TriggerTask triggerTask){
    if (taskFutures.containsKey(taskId)) {
        throw new SchedulingException("the taskId[" + taskId + "] was added.");
    }
    TaskScheduler taskScheduler = taskRegistrar.getScheduler();
    ScheduledFuture<?> future = taskScheduler.schedule(triggerTask.getRunnable(), triggerTask.getTrigger());
    taskFutures.put(taskId, future);
}

}

调用:
new Thread(()->{scheduleConfig.addTriggerTask( taskId
,new TriggerTask(()->{
//问题: 请问此线程中如何取到 taskId的值??????
},new CronTrigger("/5 * * * ?")));}).start();

落孤秋叶的主页 落孤秋叶 | 初学一级 | 园豆:106
提问于:2020-03-25 21:44
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册