SpringBoot学习笔记二十二、异步任务Async

  • Application类添加注解启用异步任务

@EnableAsync

  • 异步任务实例

package com.example.demo.task;

import java.util.Date;
import java.util.concurrent.Future;

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

@Component
@Async
public class AsyncTask {
    
    public void task1() throws InterruptedException{
        Thread.sleep(1000);
        System.out.println("[task1]执行完毕,时间:" + new Date() );
    }
    
    public void task2() throws InterruptedException{
        Thread.sleep(2000);
        System.out.println("[task2]执行完毕,时间:" + new Date() );
    }
    
    public void task3() throws InterruptedException{
        Thread.sleep(3000);
        System.out.println("[task3]执行完毕,时间:" + new Date() );
    }
    
    public Future<String> task4() throws InterruptedException{
        Thread.sleep(1000);
        System.out.println("[task4]执行完毕,时间:" + new Date() );
        return new AsyncResult<String>("[task4]执行完毕,时间:" + new Date()); 
    }
    
    public Future<String> task5() throws InterruptedException{
        Thread.sleep(2000);
        System.out.println("[task5]执行完毕,时间:" + new Date() );
        return new AsyncResult<String>("[task5]执行完毕,时间:" + new Date()); 
    }
    
    public Future<String> task6() throws InterruptedException{
        Thread.sleep(3000);
        System.out.println("[task6]执行完毕,时间:" + new Date() );
        return new AsyncResult<String>("[task6]执行完毕,时间:" + new Date()); 
    }

}
 

  • Controller类

 

package com.example.demo.controller;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.domain.JsonData;
import com.example.demo.task.AsyncTask;

@RestController
@RequestMapping("/async")
public class AsyncTaskController {

    @Autowired
    private AsyncTask asyncTask;
    
    /**
     * 功能描述: 异步任务测试1,只异步调用无需返回值
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/test1")
    public Object test1() throws InterruptedException{
        long begin = System.currentTimeMillis();
        asyncTask.task1();
        asyncTask.task2();
        asyncTask.task3();
        long end = System.currentTimeMillis();
        System.out.println("执行完毕,消耗时间="+ (end-begin));
        return JsonData.buildSuccess() ;
    }
    
    /**
     * 功能描述: 异步任务测试2,异步调用并获取返回值
     * @return
     * @throws InterruptedException
     * @throws ExecutionException 
     */
    @RequestMapping("/test2")
    public Object test2() throws InterruptedException, ExecutionException{
        long begin = System.currentTimeMillis();
        Future<String> task4= asyncTask.task4();
        Future<String> task5= asyncTask.task5();
        Future<String> task6= asyncTask.task6();
        while( !(task4.isDone()&&task5.isDone()&&task6.isDone()) ){ }
        long end = System.currentTimeMillis();
        System.out.println("执行完毕,消耗时间="+ (end-begin));
        System.out.println(task4.get());
        return JsonData.buildSuccess(end-begin) ;
    }
    
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值