隨筆 springboot @Async註解

在springboot中使用異步線程,需使用@Async註解。

在啓動類上需增加@EnableAsync(proxyTargetClass = true),proxyTargetClass爲選擇何種代理,

@SpringBootApplication
@EnableAsync(proxyTargetClass = true)
public class ChuanApplication {

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

}

controller:

@RestController
@RequestMapping("test")
public class AsyncController {
    @Autowired
    public AsyncTest asyncTest;

    @RequestMapping(value = "testAsync",method = RequestMethod.GET)
    public void testAsync(){
        System.out.println("當前時間:"+LocalTime.now());
        List<Future<String>> list =new ArrayList<>();
        try {
            asyncTest.threadTest1();
            Future<String>  future2= asyncTest.threadTest2();
            list.add(future2);
            Future<String>  future3 =asyncTest.threadTest3();
            list.add(future3);
            Thread.sleep(1000);
            System.out.println("執行阻塞!");
            list.stream().forEach(future-> {
                try {
                    System.out.println(future.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            });
        }catch (Exception e){
         e.printStackTrace();
        }
        System.out.println("結束時間:"+LocalTime.now());
    }
}

service:

@Service
public class AsyncTest {

    @Async
    public void threadTest1(){
        System.out.println("當前線程名:"+Thread.currentThread().getName());
       try{
           Thread.sleep(5000);
           System.out.println("線程:"+Thread.currentThread().getName()+"執行結束,時間爲:"+ LocalTime.now());
       }catch (Exception e){
           e.printStackTrace();
       }
    }
    @Async
    public Future<String> threadTest2(){
        System.out.println("當前線程名:"+Thread.currentThread().getName());
        try{
            Thread.sleep(3000);
            System.out.println("線程:"+Thread.currentThread().getName()+"執行結束,時間爲:"+ LocalTime.now());

        }catch (Exception e){
            e.printStackTrace();
        }
        return new AsyncResult<>("成功!"+Thread.currentThread().getName());
    }
    @Async
    public Future<String> threadTest3(){
        System.out.println("當前線程名:"+Thread.currentThread().getName());
        try{
            Thread.sleep(4000);
            System.out.println("線程:"+Thread.currentThread().getName()+"執行結束,時間爲:"+ LocalTime.now());
        }catch (Exception e){
            e.printStackTrace();
        }
        return new AsyncResult<>("成功!"+Thread.currentThread().getName());
    }
}

運行結果:

當前時間:18:18:25.625
當前線程名:task-3
當前線程名:task-4
當前線程名:task-5
執行阻塞!
線程:task-4執行結束,時間爲:18:18:28.638
成功!task-4
線程:task-5執行結束,時間爲:18:18:29.638
成功!task-5
結束時間:18:18:29.638
線程:task-3執行結束,時間爲:18:18:30.638

在這裏進行一個實驗,將threadTest2()和threadTest3()進行阻塞,阻塞是通過future.get()方法實現。threadTest1()則不進行阻塞。

如果異步返回,需返回Future<T>,return爲AsyncResult<T>。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章