Use spring asynchronous mode @EnableAsync and @Async with Future AsyncResult and asynchronous service calls in parallel, can also accelerate parallel sql query system

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/c5113620/article/details/90035551

spring boot project
notes @EnableAsync open @Async comment on the scanning method on the service class, while others call this bean is @Async annotated method, spring will be by proxy , in the sub-thread execution to achieve the purpose and implementation of parallel asynchronous call

[ Note ]

  1. You can not call @Async (is to) within the class (bean inside, spring can not be perceived), spring can not be perceived, it is no longer a sub-thread execution
    @Async take effect needs a bean (controller) calls another bean (service) approach
  2. If there is a return value, the return value is the type of packaging Future
//controller.java

    @Autowired
    private FakeService fakeService;

    @GetMapping("/slow")
    public String slow() {

        StopWatch watch = new StopWatch("my watch");

        //通常模式service调用
        watch.start("slowServiceNormal");
        fakeService.slowService();
        fakeService.slowService();
        watch.stop();

        //异步模式,在子线程各自独立调用service,有返回值
        watch.start("slowService async with return");
        Future<Long> res1 = fakeService.slowServiceAsync();
        Future<Long> res2 = fakeService.slowServiceAsync();

        try {
            res1.get();
            res2.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        watch.stop();

        //无返回值调用,直接返回,子线程继续工作
        watch.start("slowServiceNoReturn");
        fakeService.slowServiceNoReturn();
        fakeService.slowServiceNoReturn();
        watch.stop();

        System.out.println(watch.prettyPrint());

        return "done";
    }
package boottest;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author zhanghui
 * @date 2019/5/9
 */
@Service
@EnableAsync
public class FakeService {

    public long slowService(){
        System.out.println("slowServiceNormal");
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
        System.out.println("slowServiceNormal done");
        return 5;
    }

    //异步调用,有返回值,必须是Future类型,不然报错
    @Async
    public Future<Long> slowServiceAsync(){
        System.out.println("slowServiceAsync");
        return new AsyncResult(slowService());
    }

    @Async
    public void slowServiceNoReturn(){
        System.out.println("slowServiceNoReturn");
        slowService();
    }
}

Guess you like

Origin blog.csdn.net/c5113620/article/details/90035551