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

Springboot實(shí)現(xiàn)多線程及線程池監(jiān)控

 更新時(shí)間:2024年01月31日 11:03:50   作者:i學(xué)無(wú)止境  
線程池的監(jiān)控很重要,本文就來(lái)介紹一下Springboot實(shí)現(xiàn)多線程及線程池監(jiān)控,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

線程池的優(yōu)點(diǎn)

  • 降低資源消耗。通過(guò)重復(fù)利用已創(chuàng)建的線程降低線程創(chuàng)建和銷毀造成的消耗
  • 提高響應(yīng)速度。當(dāng)任務(wù)到達(dá)時(shí),任務(wù)可以不需要等到線程創(chuàng)建就能立即執(zhí)行
  • 可以對(duì)線程做統(tǒng)一管理。

1.配置線程池

修改配置文件

# 異步線程配置
# 配置核心線程數(shù)
async:
  executor:
    thread:
      core_pool_size: 5 # 配置核心線程數(shù)
      max_pool_size: 5 # 配置最大線程數(shù)
      queue_capacity: 99999 # 配置隊(duì)列大小
      name:
        prefix: async-service- # 配置線程池中的線程的名稱前

線程池配置類

package com.bt.springboot.config;

import com.bt.springboot.task.ThreadPoolMonitor;
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;

/**
 * @author zkx
 * @Date 2022/12/6 21:42
 */
@Slf4j
@Configuration
@EnableAsync
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 ThreadPoolTaskExecutor();
		//配置核心線程數(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)用者所在的線程來(lái)執(zhí)行
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		//執(zhí)行初始化
		executor.initialize();
		return executor;
	}
}

2.監(jiān)控類

package com.bt.springboot.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: ChenBin
 */
@Slf4j
@Component
public class ThreadPoolMonitor extends ThreadPoolTaskExecutor {

	@Value("${async.executor.thread.name.prefix}")
	private String namePrefix;

	@Scheduled(cron = "0/1 * * * * ? ")
	private void showThreadPoolInfo() {
		ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
		if (namePrefix.equals(this.getThreadNamePrefix())){
			log.info("{} taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]",
					this.getThreadNamePrefix(),
					threadPoolExecutor.getTaskCount(),
					threadPoolExecutor.getCompletedTaskCount(),
					threadPoolExecutor.getActiveCount(),
					threadPoolExecutor.getQueue().size());
		}
	}
}

開啟定時(shí)任務(wù)

在這里插入圖片描述

3.修改線程池配置類

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

修改為ThreadPoolTaskExecutor executor = new ThreadPoolMonitor();

4.測(cè)試相關(guān)類

AsyncService

package com.bt.springboot.async;

/**
 * @author zkx
 * @Date 2022/12/6 21:47
 */
public interface AsyncService {

	/**
	 * 執(zhí)行異步任務(wù)
	 * 可以根據(jù)需求,自己加參數(shù)擬定,我這里就做個(gè)測(cè)試演示
	 */
	void executeAsync();
}

AsyncServiceImpl

package com.bt.springboot.async.impl;

import com.bt.springboot.async.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author zkx
 * @Date 2022/12/6 21:48
 */
@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {



	@Override
	@Async("asyncServiceExecutor")
	public void executeAsync() {
		int i = 5;
		while(i > 0){
			i--;
			log.info("execute task");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				Thread.currentThread().interrupt();
			}
		}
	}
}

AsyncController

package com.bt.springboot.web.controller;

import com.bt.springboot.async.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zkx
 * @Date 2022/12/9 17:38
 */
@RestController
public class AsyncController {

	@Autowired
	private AsyncService asyncService;

	@GetMapping("/asyncTask")
	public void asyncTask(){
		asyncService.executeAsync();
	}
}

5.啟動(dòng)

1.執(zhí)行任務(wù)前

2.執(zhí)行任務(wù)

在這里插入圖片描述

3.任務(wù)完成

在這里插入圖片描述

到此這篇關(guān)于Springboot實(shí)現(xiàn)多線程及線程池監(jiān)控的文章就介紹到這了,更多相關(guān)Springboot 多線程及線程池監(jiān)控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Java中List的使用方法簡(jiǎn)單介紹

    Java中List的使用方法簡(jiǎn)單介紹

    這篇文章主要針對(duì)Java中List的使用方法為大家介紹了進(jìn)行簡(jiǎn)單介紹,List是個(gè)集合接口,只要是集合類接口都會(huì)有個(gè)“迭代子”( Iterator ),利用這個(gè)迭代子,就可以對(duì)list內(nèi)存的一組對(duì)象進(jìn)行操作,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 詳談Java中的事件監(jiān)聽機(jī)制

    詳談Java中的事件監(jiān)聽機(jī)制

    下面小編就為大家?guī)?lái)一篇詳談Java中的事件監(jiān)聽機(jī)制。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs

    詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs

    這篇文章主要介紹了詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • PowerJob的GridFsManager工作流程源碼解讀

    PowerJob的GridFsManager工作流程源碼解讀

    這篇文章主要為大家介紹了PowerJob的GridFsManager工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • JNI語(yǔ)言基本知識(shí)

    JNI語(yǔ)言基本知識(shí)

    JNI是Java Native Interface的縮寫,它提供了若干的API實(shí)現(xiàn)了Java和其他語(yǔ)言的通信(主要是C&C++)。接下來(lái)通過(guò)本文給大家分享jni 基礎(chǔ)知識(shí),感興趣的朋友一起看看吧
    2017-10-10
  • JAVA?IDEA項(xiàng)目打包為jar包的步驟詳解

    JAVA?IDEA項(xiàng)目打包為jar包的步驟詳解

    在Java開發(fā)中我們通常會(huì)將我們的項(xiàng)目打包成可執(zhí)行的Jar包,以便于在其他環(huán)境中部署和運(yùn)行,下面這篇文章主要給大家介紹了關(guān)于JAVA?IDEA項(xiàng)目打包為jar包的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • springboot集成gzip和zip數(shù)據(jù)壓縮傳輸(適用大數(shù)據(jù)信息傳輸)

    springboot集成gzip和zip數(shù)據(jù)壓縮傳輸(適用大數(shù)據(jù)信息傳輸)

    ?在大數(shù)據(jù)量的傳輸中,壓縮數(shù)據(jù)后進(jìn)行傳輸可以一定程度的解決速度問(wèn)題,本文主要介紹了springboot集成gzip和zip數(shù)據(jù)壓縮傳輸,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Spring6整合JUnit的詳細(xì)步驟

    Spring6整合JUnit的詳細(xì)步驟

    這篇文章主要介紹了Spring6整合JUnit的詳細(xì)步驟,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Java中csv文件讀寫超詳細(xì)分析

    Java中csv文件讀寫超詳細(xì)分析

    CSV是一種通用的、相對(duì)簡(jiǎn)單的文件格式,其文件以純文本形式存儲(chǔ)表格數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Java中csv文件讀寫分析的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登錄與退出功能

    Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登錄與退出功能

    這篇文章主要介紹了Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登陸與退出,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08

最新評(píng)論