欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot?@Async?注解如何實(shí)現(xiàn)方法異步

 更新時(shí)間:2021年11月20日 17:10:42   作者:Bug布道師  
這篇文章主要介紹了springboot?@Async?注解如何實(shí)現(xiàn)方法異步,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@Async注解如何實(shí)現(xiàn)方法異步

處理大批量數(shù)據(jù)的時(shí)候,效率很慢。所以考慮一下使用多線程。

剛開始自己手寫的一套,用了線程池啟動(dòng)固定的線程數(shù)進(jìn)行跑批。但是后來老大考慮到自己手寫的風(fēng)險(xiǎn)不好控制,所以使用spring的方法。

這里沒有詳細(xì)介紹,只有簡(jiǎn)單的demo,只會(huì)用,不懂原理:

一、springboot的App類需要的注解

package com.xxx.xxx.xxx;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
 * 類功能說明:服務(wù)生產(chǎn)者啟動(dòng)類
 * <p>
 * <strong></strong>
 * </p>
 *
 * @version
 * @author
 * @since 1.8
 */
@Configuration
@EnableAsync
public class Application extends SpringBootServletInitializer { 
    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 設(shè)置核心線程數(shù)
        executor.setCorePoolSize(5);
        // 設(shè)置最大線程數(shù)
        executor.setMaxPoolSize(60);
        // 設(shè)置隊(duì)列容量
        executor.setQueueCapacity(20);
        // 設(shè)置線程活躍時(shí)間(秒)
        executor.setKeepAliveSeconds(60);
        // 設(shè)置默認(rèn)線程名稱
        executor.setThreadNamePrefix("what-");
        // 設(shè)置拒絕策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任務(wù)結(jié)束后再關(guān)閉線程池
        executor.setWaitForTasksToCompleteOnShutdown(true);        
        return executor;
    }    
}

springboot的App類,很簡(jiǎn)單,就能使用很多東西。

二、service層的注解

package com.xxx.xxx.service.impl; 
import java.util.concurrent.Future; 
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service; 
import com.xxx.xxx.service.XXXAsyncService ;
 
@Service
public class XXXAsyncServiceImpl implements XXXAsyncService { 
    @Async
    public Future<Long> rtn1() throws Exception {
        //do something
        //有返回值的時(shí)候,可以返回string,long之類的。
        return new AsyncResult<>(1);
    } 
    @Async
    public void rtn2() throws Exception {
        //do something
        //這個(gè)可以沒有返回值.
    }
}

三、調(diào)用層

package com.xxx.xxx.controller; 
import java.util.concurrent.Future; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; 
import com.xxx.xxx.service.XXXAsyncService;
 
@RestController
@RequestMapping(value="/xxx") 
public class XXXAsyncController { 
 @Autowired
 private XXXAsyncService xxxAsyncService; 
 /**
  * 這里調(diào)用異步方法
  */
    @RequestMapping(value = "/xxx")
 public void dodo() throws Exception {     
  int threads = 10;//十個(gè)線程
        List<Future<Long>> list = new ArrayList<>();
        for(int i = 0;i < threads; i++){
         //這里循環(huán)調(diào)用異步方法。
      //如果存在大量數(shù)據(jù),可以在這里把數(shù)據(jù)切片,然后循環(huán)調(diào)用,分批處理數(shù)據(jù)。效率杠杠的。
   list .add(xxxAsyncService.rtn1());
        }
        long count = 0;
        for(Future<Long> l : tsfCountList) {
         //異步調(diào)用需要返回值的時(shí)候,這里可以把返回值都放入到list集合中,然后可以統(tǒng)一處理。 這里的get()是阻塞的,因?yàn)樾枰援惒椒椒ǚ祷兀诶^續(xù)執(zhí)行。
         count += l.get();
        }
        System.out.println("調(diào)用次數(shù):" + count);
 }
}

這些代碼全是手寫,記錄下來,以后用的時(shí)候,省的忘了,查起來麻煩。。

異步注解@Async的使用以及注意事項(xiàng)

第一步開啟異步

@Configuration
@EnableAsync
public class SpringAsyncConfig { ... }

默認(rèn)情況下,@EnableAsync檢測(cè)Spring的@Async注釋和EJB 3.1 javax. EJB .異步;此選項(xiàng)還可用于檢測(cè)其他用戶定義的注釋類型。(也可以在SpringBoot的啟動(dòng)類上直接加@EnableAsync注解)

在 Spring 中,用 @Async 注解指定的方法,該方法被調(diào)用時(shí)會(huì)以異步的方式執(zhí)行。而如果沒有在 @Async 注解中指定線程池,就會(huì)使用默認(rèn)的線程池。默認(rèn)的線程池為 SimpleAsyncTaskExecutor 。

該線程池不會(huì)復(fù)用線程,每有一個(gè)新任務(wù)被提交,該線程池就會(huì)創(chuàng)建一個(gè)新的線程實(shí)例用于執(zhí)行任務(wù)。下面為相關(guān)的代碼:

protected void doExecute(Runnable task) {
    Thread thread = (this.threadFactory != null ? this.threadFactory.newThread(task) : createThread(task));
    thread.start();
}

而如果想要指定線程池,可以通過在 @Async 注解中的 value 參數(shù)中指定所要使用的線程池的 Bean Name 。另一種方法是是一個(gè)實(shí)現(xiàn)了 AsyncConfigurer 接口或是繼承其默認(rèn)適配器類 AsyncConfigurerSupport 的配置類,這樣 @Async 注解的方法就會(huì)使用指定的自定義的線程池。

使用@Async注解的話采用的是springBoot默認(rèn)的線程池,不過一般我們會(huì)自定義線程池(因?yàn)楸容^靈活),配置方式有:

  • 使用 xml 文件配置的方式
  • 使用Java代碼結(jié)合@Configuration進(jìn)行配置(推薦使用)

下面顯示配置線程的代碼實(shí)現(xiàn)

package com.deppon.ptos.load.config; 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
 
/**
 * @Description: 異步線程管理
 * @Author:   LYH
 * @CreateDate: 2019/6/27 8:54
 * @Version: 1.0
 * @JDK: 1.8
 */
@Configuration
@EnableAsync
@Slf4j
public class ExecutorConfig {  
    @Value("${async.executor.thread.core_pool_size}")
    private int corePoolSize;
    @Value("${async.executor.thread.max_pool_size}")
    private int maxPoolSize;
    @Value("${async.executor.thread.queue_capacity}")
    private int queueCapacity;
    @Value("${async.executor.thread.name.prefix}")
    private String namePrefix;
 
    @Bean(name = "asyncServiceExecutor")
    public Executor asyncServiceExecutor() {
        log.info("start asyncServiceExecutor");
        ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
        //配置核心線程數(shù)
        executor.setCorePoolSize(corePoolSize);
        //配置最大線程數(shù)
        executor.setMaxPoolSize(maxPoolSize);
        //配置隊(duì)列大小
        executor.setQueueCapacity(queueCapacity);
        //配置線程池中的線程的名稱前綴
        executor.setThreadNamePrefix(namePrefix);
 
        // rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)
        // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來執(zhí)行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //執(zhí)行初始化
        executor.initialize();
        return executor;
    }
}
package com.deppon.ptos.load.config;  
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture; 
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
 
/**
 * @Description: 打印異步線程的執(zhí)行情況   使用Callbale Future 來返回線程的信息
 * @Author: 633805  LYH
 * @CreateDate: 2019/6/27 8:59
 * @Version: 1.0
 * @JDK: 1.8
 */
@Component
@Slf4j
public class VisiableThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {  
    private void showThreadPoolInfo(String prefix) {
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor(); 
        if (null == threadPoolExecutor) {
            return;
        }
 
        log.info("{}, {},taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]",
                this.getThreadNamePrefix(),
                prefix,
                threadPoolExecutor.getTaskCount(),
                threadPoolExecutor.getCompletedTaskCount(),
                threadPoolExecutor.getActiveCount(),
                threadPoolExecutor.getQueue().size());
    }
 
    @Override
    public void execute(Runnable task) {
        showThreadPoolInfo("1. do execute");
        super.execute(task);
    }
 
    @Override
    public void execute(Runnable task, long startTimeout) {
        showThreadPoolInfo("2. do execute");
        super.execute(task, startTimeout);
    }
 
    @Override
    public Future<?> submit(Runnable task) {
        showThreadPoolInfo("1. do submit");
        return super.submit(task);
    }
 
    @Override
    public <T> Future<T> submit(Callable<T> task) {
        showThreadPoolInfo("2. do submit");
        return super.submit(task);
    }
 
    @Override
    public ListenableFuture<?> submitListenable(Runnable task) {
        showThreadPoolInfo("1. do submitListenable");
        return super.submitListenable(task);
    }
 
    @Override
    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
        showThreadPoolInfo("2. do submitListenable");
        return super.submitListenable(task);
    }
}

使用:

@Async("asyncServiceExecutor")

到這一步,異步就算開啟了。

下面主要說一說錯(cuò)誤的

使用@Async導(dǎo)致異步不成功的情況

如下方式會(huì)使@Async失效

  • 異步方法使用static修飾
  • 異步類沒有使用@Component注解(或其他注解)導(dǎo)致spring無法掃描到異步類
  • 異步方法不能與被調(diào)用的異步方法在同一個(gè)類中
  • 類中需要使用@Autowired或@Resource等注解自動(dòng)注入,不能自己手動(dòng)new對(duì)象
  • 如果使用SpringBoot框架必須在啟動(dòng)類中增加@EnableAsync注解

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java 格式化時(shí)間的示例代碼

    java 格式化時(shí)間的示例代碼

    這篇文章主要介紹了java 格式化時(shí)間的示例代碼,幫助大家更好的利用Java處理時(shí)間,感興趣的朋友可以了解下
    2020-12-12
  • Java 給PPT添加動(dòng)畫效果的示例

    Java 給PPT添加動(dòng)畫效果的示例

    這篇文章主要介紹了Java 給PPT添加動(dòng)畫效果的示例,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04
  • Netty分布式抽象編碼器MessageToByteEncoder邏輯分析

    Netty分布式抽象編碼器MessageToByteEncoder邏輯分析

    這篇文章主要介紹了Netty分布式抽象編碼器MessageToByteEncoder的抽象邏輯分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Spring Boot集成Ehcache緩存解決方式

    Spring Boot集成Ehcache緩存解決方式

    在本篇文章里小編給大家整理的是關(guān)于Spring Boot集成Ehcache緩存解決方式,需要的朋友們可以學(xué)習(xí)下。
    2019-12-12
  • 詳解Java編程中向量(Vector)的應(yīng)用

    詳解Java編程中向量(Vector)的應(yīng)用

    這篇文章主要介紹了Java中的向量(Vector)的應(yīng)用,Vector也是Java中比較常用的一個(gè)類,需要的朋友可以參考下
    2015-10-10
  • JAVA注解代碼詳解一篇就夠了

    JAVA注解代碼詳解一篇就夠了

    這篇文章主要介紹了Java注解詳細(xì)介紹,本文講解了Java注解是什么、Java注解基礎(chǔ)知識(shí)、Java注解類型、定義Java注解類型的注意事項(xiàng)等內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • SpringBoot中的事務(wù)配置管理詳解

    SpringBoot中的事務(wù)配置管理詳解

    這篇文章主要介紹了SpringBoot中的事務(wù)配置管理詳解,Spring Boot 默認(rèn)的事務(wù)規(guī)則是遇到運(yùn)行異常(RuntimeException)和程序錯(cuò)誤(Error)才會(huì)回滾,但是拋出SQLException就無法回滾了,需要的朋友可以參考下
    2024-01-01
  • Springboot?手動(dòng)分頁查詢分批批量插入數(shù)據(jù)的實(shí)現(xiàn)流程

    Springboot?手動(dòng)分頁查詢分批批量插入數(shù)據(jù)的實(shí)現(xiàn)流程

    這篇文章主要介紹了Springboot?手動(dòng)分頁查詢分批批量插入數(shù)據(jù)的實(shí)現(xiàn)流程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • SpringBoot項(xiàng)目打包為JAR文件的實(shí)現(xiàn)

    SpringBoot項(xiàng)目打包為JAR文件的實(shí)現(xiàn)

    本文主要介紹了SpringBoot項(xiàng)目打包為JAR文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-09-09
  • 詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知

    詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知

    本篇文章主要介紹了詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12

最新評(píng)論