Java异步创建线程池newFixedThreadPool

ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);
@RequestMapping(value = "test", method = RequestMethod.POST)
public String test() {

    String result = "true";
    if(false) {//你的错误异常情况自行判断加入条件
        result = "false";
        return result;
    }else {//正确的
       
        newFixedThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(5000);

                    //你的业务逻辑异步要触发的
                    System.out.println("dafsdfadsfasfdsfasd阿是范德萨发");//伪代码
                } catch (InterruptedException e) {
                    e.printStackTrace()
                }
             }
        });
         newFixedThreadPool.shutdown();
        return result;
    }

}
@PreDestroy
public void close() {//销毁前调用
   try {
      newFixedThreadPool.shutdown();
      newFixedThreadPool.awaitTermination(5, TimeUnit.MINUTES);
   } catch (InterruptedException e) {
      log.error("unexpected: close push thread, may lost task ,cause user create excel or send email error ", e);
   }
}

 

你可能感兴趣的:(java)