【发布时间】:2016-03-29 23:07:12
【问题描述】:
我正在尝试使用 Spring 的“ThreadPoolTaskExecutor”实现多线程连续任务执行。
这是我的班级:
@Component
public class AsyncWorker implements Worker {
public int threadCount = 5;
private ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
@Async
public void doWork(Runnable runnable){
executor.initialize();
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setCorePoolSize(threadCount);
executor.setMaxPoolSize(threadCount);
executor.setQueueCapacity(0);
executor.setThreadGroupName("A");
for (int i=0;i<5;i++) {
executor.submit(runnable);
}
System.out.println("Active threads: " +executor.getActiveCount());
}
@Async
public void StopTasks(){
executor.shutdown();
}
用法:
@Controller
@RequestMapping("api/test")
public class SendController {
ThreadPoolExecutor executor = new ErrorReportingThreadPoolExecutor(5);
@Autowired AsyncWorker worker;
boolean IsRunning = true;
@RequestMapping(value = "/start_new", method = RequestMethod.POST)
public Callable<String> StartNewTask(@RequestBody LaunchSend sendobj) throws IOException, InterruptedException {
// System.out.println(sendobj.getThreadsCount());
Runnable runnable = () -> {
while(IsRunning) {
MyVoid();
}
};
worker.doWork(runnable);
return () -> "Callable result";
}
@RequestMapping(value = "/stop", method = RequestMethod.GET)
public Callable<String> StopTasks() {
IsRunning =false;
if(SecurityContextHolder.getContext().getAuthentication().getName() != null && SecurityContextHolder.getContext().getAuthentication().getName() != "anonymousUser") {
worker.StopTasks();
return () -> "Callable result good";
}
else { return () -> "Callable result bad";}
}
}
主要问题是:
当我第一次发送启动请求时 - 一切正常(除非我没有看到组名称的变化(executor.setThreadGroupName("A")))。
但是,在我发送停止请求并再次发送启动请求后 - 执行程序不会启动任务。这是它的样子:
ThreadPoolTaskExecutor-4 Global iteration # 4
ThreadPoolTaskExecutor-2 Global iteration # 5
ThreadPoolTaskExecutor-5 Global iteration # 3
ThreadPoolTaskExecutor-3 Global iteration # 2
Active threads: 5
ThreadPoolTaskExecutor-1 Global iteration # 1
ThreadPoolTaskExecutor-2 Global iteration # 9
ThreadPoolTaskExecutor-3 Global iteration # 9
ThreadPoolTaskExecutor-5 Global iteration # 9
ThreadPoolTaskExecutor-4 Global iteration # 9
ThreadPoolTaskExecutor-1 Global iteration # 10
Active threads: 1
只显示“活动线程:1”。可能是什么问题?
顺便说一句,将来,我想为它创建的 ThreadGroup 设置自定义名称(从 POST 请求/Spring Security Context 传输的参数),然后能够以某个组名终止所有线程(组名将是 Spring Security启动线程的用户的用户名。)有可能吗?
【问题讨论】:
-
仅供参考,将 boolean isRunnable 标记为 volatile,不要使用 != 运算符比较字符串,使用 equals() 方法
标签: java multithreading spring java.util.concurrent