成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

springboot中使用自定義線程池ThreadPoolTaskExecutor

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

    我們來看一個(gè)例子
   package com.executor;

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

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、樂東黎族網(wǎng)絡(luò)推廣、微信小程序、樂東黎族網(wǎng)絡(luò)營銷、樂東黎族企業(yè)策劃、樂東黎族品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供樂東黎族建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:jinyejixie.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 ..");
    // 設(shè)置拒絕策略
    executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            // .....
            System.out.println("do somethings by myself ...");
        }
    });
    // 使用預(yù)定義的異常處理類
    // 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("進(jìn)入service。。。");
try {
Thread.sleep(3000);
System.out.println("3S后數(shù)據(jù)開始處理中。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}

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

}

運(yùn)行一下看看

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("進(jìn)入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;
    }

}

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

文章標(biāo)題:springboot中使用自定義線程池ThreadPoolTaskExecutor
瀏覽地址:http://jinyejixie.com/article8/ppppip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)建站、定制開發(fā)、搜索引擎優(yōu)化虛擬主機(jī)、網(wǎng)站維護(hù)

廣告

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

成都網(wǎng)頁設(shè)計(jì)公司
常山县| 安顺市| 山阳县| 宜州市| 长武县| 蚌埠市| 台北市| 龙游县| 桐城市| 新泰市| 祁东县| 龙州县| 桃源县| 鹤壁市| 婺源县| 句容市| 江华| 西和县| 博白县| 会理县| 来凤县| 天台县| 凉山| 曲靖市| 惠水县| 涡阳县| 娱乐| 武穴市| 楚雄市| 青田县| 新丰县| 西青区| 丽水市| 项城市| 南部县| 互助| 罗源县| 新干县| 晋宁县| 远安县| 岳阳县|