亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

線程池的優(yōu)點

  • 降低資源消耗。通過重復利用已創(chuàng)建的線程降低線程創(chuàng)建和銷毀造成的消耗
  • 提高響應速度。當任務到達時,任務可以不需要等到線程創(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已經達到max size的時候,如何處理新任務
		// CALLER_RUNS:不在新線程中執(zhí)行任務,而是有調用者所在的線程來執(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());
		}
	}
}

開啟定時任務

在這里插入圖片描述

3.修改線程池配置類

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

修改為ThreadPoolTaskExecutor executor = new ThreadPoolMonitor();

4.測試相關類

AsyncService

package com.bt.springboot.async;

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

	/**
	 * 執(zhí)行異步任務
	 * 可以根據(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í)行任務前

2.執(zhí)行任務

在這里插入圖片描述

3.任務完成

在這里插入圖片描述

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

相關文章

  • Java中List的使用方法簡單介紹

    Java中List的使用方法簡單介紹

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

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

    下面小編就為大家?guī)硪黄斦凧ava中的事件監(jiān)聽機制。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 詳解spring cloud整合Swagger2構建RESTful服務的APIs

    詳解spring cloud整合Swagger2構建RESTful服務的APIs

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

    PowerJob的GridFsManager工作流程源碼解讀

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

    JNI語言基本知識

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

    JAVA?IDEA項目打包為jar包的步驟詳解

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

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

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

    Spring6整合JUnit的詳細步驟

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

    Java中csv文件讀寫超詳細分析

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

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

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

最新評論