ExecutorService 异步线程池

线程池 ExecutorService 的使用例子

Java多线程-Callable的Future返回值的使用

ExecutorService

@RestController
@RequestMapping("/hello")
public class HelloController {

	private ExecutorService service = Executors.newFixedThreadPool(10);

    @GetMapping
    public String hello() {
        service.execute(new Runnable() {
    
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                System.out.println("我醒了");
            }
        });
    
        return "hello world";
    }
}

实例理解:

会先返回"hello world", 3秒钟后控制台输出"我醒了"

Last updated

Was this helpful?