ThreadPoolTask​​Scheduler运行任务,为什么将来是相同的?

时间:2018-12-27 05:55:46

标签: java threadpool

这是 <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Modal Example</h2> <!-- Trigger the modal with a button --> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <!-- Modal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <select required id="ddlcolors" class="text-success form-control input-sm"> <option class="text-success" selected disabled value=""> <h6>-- Select Colors --</h6> </option> <option class="text-success text-center" value="R">Red</option> <option class="text-success text-center" value="G">Green</option> <option class="text-success text-center" value="B">Blue</option> <option class="text-success text-center" value="O">Orange</option> </select> <input type="text" readonly id="txtmanuid" > </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> <script> $( "#ddlcolors" ).change(function() { var sel = $( "#ddlcolors option:selected" ).val(); var txtman = $( "#ddlcolors option:selected" ).text() $('#txtmanuid').val(txtman); }); </script> </body> </html>代码:

treadpooltaskscheduler

这是任务启动和停止服务代码:

@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;

private ScheduledFuture<?> future;

@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
     threadPoolTask.setPoolSize(10);
     return new ThreadPoolTaskScheduler();
}

当我运行应用程序并运行2个任务并且打印“未来”时,我发现“未来”是相同的

4 个答案:

答案 0 :(得分:0)

我认为您只需要一个

Thread.sleep(10000) 

答案 1 :(得分:0)

默认threadPoolSize =1。因此,您的任务在一个线程中运行。

您应使用setPoolSize设置线程池大小。

答案 2 :(得分:0)

ThreadPoolTaskScheduler不是Java提供的类。因此,我认为这是第三方课程还是您自己的课程。处理此问题的正确位置在您的代码中:

@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
     return new ThreadPoolTaskScheduler();
}

如果类ThreadPoolTaskScheduler的构造函数采用线程数量(应如此),只需在该位置指定它,或者如果存在单独的setter方法来设置线程数量,则不更改上面的代码以首先创建一个实例,然后设置线程数。就像这样:

@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
     return new ThreadPoolTaskScheduler(10);
}


@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
     ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
     threadPoolTaskScheduler.setThreads(10)
     return threadPoolTaskScheduler;
}

答案 3 :(得分:0)

情况1:您的ThreadPoolTask​​Scheduler大小为1

情况2:您的任务执行时间太短

您可以提供更多代码吗?