最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

線程池的優(yōu)點

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

1.配置線程池

修改配置文件

# 異步線程配置
# 配置核心線程數(shù)
async:
  executor:
    thread:
      core_pool_size: 5 # 配置核心線程數(shù)
      max_pool_size: 5 # 配置最大線程數(shù)
      queue_capacity: 99999 # 配置隊列大小
      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);
		//配置隊列大小
		executor.setQueueCapacity(queueCapacity);
		//配置線程池中的線程的名稱前綴
		executor.setThreadNamePrefix(namePrefix);

		// rejection-policy:當pool已經(jīng)達到max size的時候,如何處理新任務(wù)
		// CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來執(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());
		}
	}
}

開啟定時任務(wù)

在這里插入圖片描述

3.修改線程池配置類

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

修改為ThreadPoolTaskExecutor executor = new ThreadPoolMonitor();

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

AsyncService

package com.bt.springboot.async;

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

	/**
	 * 執(zhí)行異步任務(wù)
	 * 可以根據(jù)需求,自己加參數(shù)擬定,我這里就做個測試演示
	 */
	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.啟動

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

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

在這里插入圖片描述

3.任務(wù)完成

在這里插入圖片描述

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

相關(guān)文章

  • 關(guān)于Mybatis和JDBC的使用及區(qū)別

    關(guān)于Mybatis和JDBC的使用及區(qū)別

    這篇文章主要介紹了關(guān)于Mybatis和JDBC的使用及區(qū)別,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • springboot的Customizer源碼解析

    springboot的Customizer源碼解析

    這篇文章主要為大家介紹了springboot的Customizer源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Java實現(xiàn)的傅里葉變化算法示例

    Java實現(xiàn)的傅里葉變化算法示例

    這篇文章主要介紹了Java實現(xiàn)的傅里葉變化算法,結(jié)合具體實例形式分析了基于Java的傅里葉變化算法定義與使用相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • 深入解析java HashMap實現(xiàn)原理

    深入解析java HashMap實現(xiàn)原理

    這篇文章主要介紹了深入解析java HashMap實現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下
    2015-09-09
  • Java實現(xiàn)二叉樹的基本操作詳解

    Java實現(xiàn)二叉樹的基本操作詳解

    這篇文章主要為大家詳細介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法中二叉樹的基本操作,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下
    2022-10-10
  • Java中的System.arraycopy()淺復(fù)制方法詳解

    Java中的System.arraycopy()淺復(fù)制方法詳解

    這篇文章主要介紹了Java中的System.arraycopy()淺復(fù)制方法詳解,Java數(shù)組的復(fù)制操作可以分為深度復(fù)制和淺度復(fù)制,簡單來說深度復(fù)制,可以將對象的值和對象的內(nèi)容復(fù)制;淺復(fù)制是指對對象引用的復(fù)制,需要的朋友可以參考下
    2023-11-11
  • SpringAop @Aspect織入不生效,不執(zhí)行前置增強織入@Before方式

    SpringAop @Aspect織入不生效,不執(zhí)行前置增強織入@Before方式

    這篇文章主要介紹了SpringAop @Aspect織入不生效,不執(zhí)行前置增強織入@Before方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java如何從linux服務(wù)器下載文件

    java如何從linux服務(wù)器下載文件

    這篇文章主要介紹了java如何從linux服務(wù)器下載文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring MVC url提交參數(shù)和獲取參數(shù)

    Spring MVC url提交參數(shù)和獲取參數(shù)

    本文重要講述通過url提交參數(shù)和獲取參數(shù)的具體操作與實現(xiàn)。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Spring中@Autowired @Resource @Inject三個注解有什么區(qū)別

    Spring中@Autowired @Resource @Inject三個注解有什么區(qū)別

    在我們使用Spring框架進行日常開發(fā)過程中,經(jīng)常會使用@Autowired, @Resource, @Inject注解來進行依賴注入,下面來介紹一下這三個注解有什么區(qū)別
    2023-03-03

最新評論

铁力市| 邵阳县| 汝南县| 屏边| 自贡市| 马公市| 马公市| 临城县| 库伦旗| 麦盖提县| 望都县| 临城县| 沅江市| 六枝特区| 白银市| 邯郸市| 自贡市| 扶沟县| 台南县| 三亚市| 通渭县| 宁南县| 麻阳| 新郑市| 贡觉县| 葫芦岛市| 安远县| 宾阳县| 望都县| 合作市| 永昌县| 泾阳县| 徐水县| 杂多县| 五家渠市| 长海县| 华安县| 高安市| 铁岭县| 灵石县| 丰都县|