Java 实现异步调用

被调用方法

package com.ruyi.async.controller;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.Random;
import java.util.concurrent.Future;

@Component
public class AsyncBean {
    /**
     * 有返回值
     * @return Future<String>
     * @throws InterruptedException
     */
    @Async
    public Future<String> taskOneWithReturn() throws InterruptedException {
        System.out.println(">>> 开始执行异步任务..");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Thread.sleep(new Random().nextInt(10000));
        stopWatch.stop();
        System.out.println("完成异步任务,耗时:" + stopWatch.getTotalTimeMillis() + "毫秒");
        return new AsyncResult<>(">>> 异步任务执行完成..");
    }

    /**
     * 无返回值
     * @throws InterruptedException
     */
    @Async
    public void taskTwoWithoutReturn() throws InterruptedException {
        System.out.println(">>> 开始执行异步任务..");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Thread.sleep(new Random().nextInt(10000));
        stopWatch.stop();
        System.out.println("完成异步任务,耗时:" + stopWatch.getTotalTimeMillis() + "毫秒");
    }
}

四种实现方式

package com.ruyi.async.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.*;
import java.util.function.Supplier;

@RestController
public class TestController {

    @Resource
    private AsyncBean asyncBean;

    /**
     * 一、Thread
     */
    @RequestMapping(value = "byThread", method = RequestMethod.POST)
    public void byThread() {
        System.out.println(">>> main函数开始执行");

        Thread thread = new Thread(() -> {
            System.out.println("===task start===");
            try {
                asyncBean.taskOneWithReturn();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("===task finish===");
        });
        thread.start();

        System.out.println(">>> main函数执行结束");
    }


    /**
     * 二、Future
     * jdk8之前的实现方式,在JUC下增加了Future,从字面意思理解就是未来的意思。
     * 但使用起来却着实有点鸡肋,并不能实现真正意义上的异步,获取结果时需要阻塞线程,或者不断轮询。
     */
    @RequestMapping(value = "byFuture", method = RequestMethod.POST)
    public void byFuture() throws Exception {
        System.out.println("main函数开始执行");

        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<String> future = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("===task start===");
                Future<String> stringFuture = asyncBean.taskOneWithReturn();
                System.out.println("===task finish===");
                return stringFuture.toString();
            }
        });
        // 如果这里需要返回值,则会阻塞主线程。如果不需要返回值使用是OK的。倒也还能接受
        String result = future.get();
        System.out.println(result);
        System.out.println("main函数执行结束");

        // 不停轮询去获取异步的执行结果
        System.in.read();
    }

    /**
     * 三、CompletableFuture
     * 使用原生的CompletableFuture实现异步操作,加上对lambda的支持,可以说实现异步任务已经发挥到了极致。
     */
    @RequestMapping(value = "byCompletableFuture", method = RequestMethod.POST)
    public void byCompletableFuture() {
        System.out.println("main函数开始执行");

        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {
            @lombok.SneakyThrows
            @Override
            public String get() {
                System.out.println("===task start===");
                Future<String> stringFuture = null;
                try {
                    stringFuture = asyncBean.taskOneWithReturn();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("===task finish===");
                assert stringFuture != null;
                return stringFuture.get();
            }
        }, executor);
        // 如果获取返回值,则完美执行,但是一旦需要返回值,那么就跟普通的方法调用没啥区别了
//        String s = future.get();
        future.thenAccept(System.out::println);
        System.out.println("main函数执行结束");
    }

    /**
     * 四、Spring注解@Async 最优解
     *  需要在启动类添加注解@EnableAsync,开启异步调用
     *  关于这个返回值,如果不要返回值,那么一切顺利,一旦需要返回值,其实就跟普通调用差不多了
     */
    @RequestMapping(value = "byAsync", method = RequestMethod.POST)
    public void byAsync() throws Exception {
        System.out.println("main函数开始执行");
        asyncBean.taskTwoWithoutReturn();  // 无返回值
        Future<String> future = asyncBean.taskOneWithReturn();  // 有返回值,可以使用 future.get() 方法获取
        System.out.println("main函数执行结束");
//        System.out.println("异步执行结果:" + future.get());
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值