0    课程地址

https://www.imooc.com/video/16793/0

 

1    课程重点
1.0  异步任务和同步对比

异步取异步任务时间最大值,同步取同步任务累计值

 

1.1  异步任务使用场景

◆发送短信

◆发送邮件

◆App消息推送

◆节省运维凌晨发布任务时间提供效率

 

1.2  异步任务使用注意事项

顶类开启异步任务开关@EnableAsync

任务类使用@Component @Async作为组件被扫描执行

 

1.3  提高效率(和异步任务类似操作)

多线程、消息队列、kfka、*MQ等等

 

1    课程重点
2.1  使用同步任务demo对比

顶类:(同下)

触发类:(同下)

任务类:

package com.example.demo.task;

import java.util.concurrent.Future;

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

@Component
public class AsyncTask {
    
    //@Async
    public Future<Boolean> doTask11() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        long end = System.currentTimeMillis();
        System.out.println("任务1耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    //@Async
    public Future<Boolean> doTask22() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(700);
        long end = System.currentTimeMillis();
        System.out.println("任务2耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    //@Async
    public Future<Boolean> doTask33() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(600);
        long end = System.currentTimeMillis();
        System.out.println("任务3耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true); 
    }
}

 

测试结果:

 

 

 

 

 

2.2  使用异步任务demo对比

顶类:

package com.example.demo;

import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

//扫描mybaties mapper包路径
@MapperScan(basePackages = "com.example.demo.mapper")

//扫描 所需要的包,包含自用的工具类包所在路径
@ComponentScan(basePackages={"com.example.demo","org.n3r.idworker"})

//开启定时任务
//@EnableScheduling

//开启异步调用方法
@EnableAsync
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

 

触发类:

package com.example.demo.task;

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;

@RestController
@RequestMapping("tasks")
public class DoTask {
    
    @Autowired
    private AsyncTask asyncTask;
    
    @RequestMapping("test1")
    public String test1() throws Exception {
        
        long start = System.currentTimeMillis();
        
        Future<Boolean> a = asyncTask.doTask11();
        Future<Boolean> b = asyncTask.doTask22();
        Future<Boolean> c = asyncTask.doTask33();
        
        while (!a.isDone() || !b.isDone() || !c.isDone()) {
            if (a.isDone() && b.isDone() && c.isDone()) {
                break;
            }
        }
        
        long end = System.currentTimeMillis();
        
        String times = "任务全部完成,总耗时:" + (end - start) + "毫秒";
        System.out.println(times);
        
        return times;
    }
}

 

任务类:

package com.example.demo.task;

import java.util.concurrent.Future;

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

@Component
public class AsyncTask {
    
    @Async
    public Future<Boolean> doTask11() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        long end = System.currentTimeMillis();
        System.out.println("任务1耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    @Async
    public Future<Boolean> doTask22() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(700);
        long end = System.currentTimeMillis();
        System.out.println("任务2耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    @Async
    public Future<Boolean> doTask33() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(600);
        long end = System.currentTimeMillis();
        System.out.println("任务3耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true); 
    }
}

 

测试结果:

 

 

 

posted on 2020-12-27 10:00  菜鸟乙  阅读(123)  评论(0编辑  收藏  举报