JAva非同期処理時間消費要求

14152 ワード

アナログ非同期呼び出し
import com.springdemo2.service.helloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

@RestController
public class HelloController2 {

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private helloService hello;

    @GetMapping("/helloworld")
    public String helloWorldController() {
        logger.info(Thread.currentThread().getName() + "   helloWorldController  ");
        String say = hello.sayHello();
        logger.info("    ");
        return say;
    }

    /**
     *     restful
     *  controller    Callable   ,springmvc         Callable  TaskExecutor   
     *   DispatcherServlet     spring         ,   response       
     *  Callable      ,springmvc          request  ,  DispatcherServlet   
     *      Callable         ,       
     *
     * @return
     */
    @GetMapping("/hello1")
    public Callable<String> helloController() {
        logger.info(Thread.currentThread().getName() + "   helloController  ");
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                logger.info(Thread.currentThread().getName() + "   call  ");
                //            
                String say = hello.sayHello();
                //
                logger.info(Thread.currentThread().getName() + "  helloService    ");
                return say;
            }
        };
        logger.info(Thread.currentThread().getName() + "  helloController    ");
        return callable;
    }
}

アナログ時間消費要求
import org.springframework.stereotype.Component;

@Component
public class helloService {

   public String sayHello(){
       try {
           Thread.sleep(10000);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return "aa";
   }
}

ログ#ログ#
helloControllerは新しいスレッド処理タスクを呼び出し、helloWorldControllerは現在のスレッド処理であり、現在のスレッドをブロックします.
2019-03-18 20:13:56.171  INFO 7495 --- [io-8080-exec-10] c.s.contorller.HelloController           : http-nio-8080-exec-10   helloController  
2019-03-18 20:13:56.171  INFO 7495 --- [io-8080-exec-10] c.s.contorller.HelloController           : http-nio-8080-exec-10  helloController    
2019-03-18 20:13:56.172  INFO 7495 --- [         task-9] c.s.contorller.HelloController           : task-9   call  
2019-03-18 20:13:57.222  INFO 7495 --- [nio-8080-exec-1] c.s.contorller.HelloController           : http-nio-8080-exec-1   helloController  
2019-03-18 20:13:57.222  INFO 7495 --- [nio-8080-exec-1] c.s.contorller.HelloController           : http-nio-8080-exec-1  helloController    
2019-03-18 20:13:57.223  INFO 7495 --- [        task-10] c.s.contorller.HelloController           : task-10   call  
2019-03-18 20:13:58.084  INFO 7495 --- [nio-8080-exec-2] c.s.contorller.HelloController           : http-nio-8080-exec-2   helloController  
2019-03-18 20:13:58.084  INFO 7495 --- [nio-8080-exec-2] c.s.contorller.HelloController           : http-nio-8080-exec-2  helloController    
2019-03-18 20:13:58.084  INFO 7495 --- [        task-11] c.s.contorller.HelloController           : task-11   call  
2019-03-18 20:14:06.176  INFO 7495 --- [         task-9] c.s.contorller.HelloController           : task-9  helloService    
2019-03-18 20:14:07.223  INFO 7495 --- [        task-10] c.s.contorller.HelloController           : task-10  helloService    
2019-03-18 20:14:08.087  INFO 7495 --- [        task-11] c.s.contorller.HelloController           : task-11  helloService    


2019-03-18 20:14:20.996  INFO 7495 --- [nio-8080-exec-7] c.s.contorller.HelloController           : http-nio-8080-exec-7   helloWorldController  
2019-03-18 20:14:22.051  INFO 7495 --- [nio-8080-exec-8] c.s.contorller.HelloController           : http-nio-8080-exec-8   helloWorldController  
2019-03-18 20:14:22.836  INFO 7495 --- [nio-8080-exec-9] c.s.contorller.HelloController           : http-nio-8080-exec-9   helloWorldController  
2019-03-18 20:16:56.858  INFO 7499 --- [nio-8080-exec-1] c.s.contorller.HelloController           : http-nio-8080-exec-1    
2019-03-18 20:16:57.424  INFO 7499 --- [nio-8080-exec-2] c.s.contorller.HelloController           : http-nio-8080-exec-2    
2019-03-18 20:16:58.058  INFO 7499 --- [nio-8080-exec-3] c.s.contorller.HelloController           : http-nio-8080-exec-3