新建了一个线程池,但是只能跑核心数量的线程,设置的最大线程数不起作用,是为什么?
@RestController
@RequestMapping("/test")
public class TestController {
private ExecutorService executor = new ThreadPoolExecutor(3, 10, 20, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(20),
new ThreadPoolExecutor.CallerRunsPolicy());
@GetMapping("/thread")
public String testThreadPool() {
executor.execute(new MyTask("一號"));
executor.execute(new MyTask("二號"));
executor.execute(new MyTask("三號"));
executor.execute(new MyTask("四號"));
executor.execute(new MyTask("五號"));
return "success";
}
}
public class MyTask implements Runnable {
private String name;
public MyTask(String name) {
this.name = name;
}
@Override
public void run() {
while (true) {
System.out.println("------ 我是任务:" + name);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}