springboot中使用自定義線程池ThreadPoolTaskExecutor

java5以后,線程有了很大的變化,在使用上更加方便功能更佳強大,Springboot里面進行了進一步的封裝。

    我們來看一個例子
   package com.executor;

import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

創新互聯建站專注為客戶提供全方位的互聯網綜合服務,包含不限于成都網站設計、做網站、樂東黎族網絡推廣、微信小程序、樂東黎族網絡營銷、樂東黎族企業策劃、樂東黎族品牌公關、搜索引擎seo、人物專訪、企業宣傳片、企業代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創新互聯建站為所有大學生創業者提供樂東黎族建站搭建服務,24小時服務熱線:18980820575,官方網址:m.kartarina.com

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import org.springframework.context.annotation.Configuration;
import
org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration@EnableAsync
br/>@EnableAsync

@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(15);
    executor.setThreadNamePrefix("Anno-Executor");
    executor.setQueueCapacity(25);
    executor.initialize();
    System.out.println("initialize complete ..");
    // 設置拒絕策略
    executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            // .....
            System.out.println("do somethings by myself ...");
        }
    });
    // 使用預定義的異常處理類
    // executor.setRejectedExecutionHandler(new
    // ThreadPoolExecutor.CallerRunsPolicy());

    return executor;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    return null;
}

}

package com.executor;

import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
public class SyncService {@Async
br/>@Async
System.out.println("進入service。。。");
try {
Thread.sleep(3000);
System.out.println("3S后數據開始處理中。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Async
public Future<String> asyncInvokeReturnFuture(int i) {
    System.out.println("進入asyncInvokeReturnFuture..." + Thread.currentThread().getName());
    Future<String> future;
    try {
        Thread.sleep(3000);
        System.out.println("3S后asyncInvokeReturnFuture數據開始處理中。。");
        future = new AsyncResult<String>("success:" + i);
    } catch (InterruptedException e) {
        future = new AsyncResult<String>("error");
    }
    return future;
}

}

運行一下看看

package com.executor;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@EnableAutoConfiguration
br/>@EnableAutoConfiguration
br/>@SpringBootApplication
br/>@EnableAsync

private final static Logger logger = LoggerFactory.getLogger(ExampleSync.class);
@Autowired
SyncService asyncService;

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

 @GetMapping("/hello")
    public String hello(){
        System.out.println("進入Controller。。。");
        asyncService.hello();
        Future<String> future=asyncService.asyncInvokeReturnFuture(300);
        String s = null;
        try {
            s = future.get();
        } catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("異步方法返回值 : "+s);
        return s;
    }

}

進入Controller。。。
進入service。。。
進入asyncInvokeReturnFuture...Anno-Executor2
3S后數據開始處理中。。
3S后asyncInvokeReturnFuture數據開始處理中。。
異步方法返回值 : success:300
進入Controller。。。
進入service。。。
進入asyncInvokeReturnFuture...Anno-Executor3
3S后數據開始處理中。。
3S后asyncInvokeReturnFuture數據開始處理中。。
異步方法返回值 : success:300

文章標題:springboot中使用自定義線程池ThreadPoolTaskExecutor
瀏覽地址:http://m.kartarina.com/article8/ppppip.html

成都網站建設公司_創新互聯,為您提供電子商務企業建站定制開發搜索引擎優化虛擬主機網站維護

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網頁設計公司
主站蜘蛛池模板: 无码熟妇αⅴ人妻又粗又大| 人妻av无码专区| 激情无码人妻又粗又大| 久久精品?ⅴ无码中文字幕| 无码人妻丰满熟妇区BBBBXXXX| 国产成人无码av| 西西4444www大胆无码| 成在线人免费无码高潮喷水| 西西大胆无码视频免费| 中日韩亚洲人成无码网站| 无码国内精品人妻少妇蜜桃视频| 亚洲国产综合无码一区| 亚洲日韩欧洲无码av夜夜摸| 无码人妻丰满熟妇区BBBBXXXX | 亚洲a无码综合a国产av中文| 亚洲中文字幕无码久久精品1 | 国产精品无码免费播放| 无码内射中文字幕岛国片| 精品无人区无码乱码大片国产| 亚洲av无码专区亚洲av不卡| 亚洲av福利无码无一区二区 | 免费无码精品黄AV电影| 无码人妻一区二区三区免费手机| 寂寞少妇做spa按摩无码| 青春草无码精品视频在线观| 啊灬啊别停灬用力啊无码视频| 亚洲AV日韩AV永久无码绿巨人 | 国产免费av片在线无码免费看| 无码日本精品XXXXXXXXX| 色欲A∨无码蜜臀AV免费播| 久久亚洲精品无码| 少妇人妻无码专区视频| 久久亚洲AV成人无码电影| 久久亚洲精品无码网站| 亚洲综合无码一区二区痴汉| 在线观看无码不卡AV| 国产精品无码免费播放| 免费无码专区毛片高潮喷水| 人妻少妇无码视频在线| 中文无码乱人伦中文视频在线V| 超清无码熟妇人妻AV在线电影|