springboot tomcat默認線程數_記一次JAVA線程池的錯誤用法

最近項目一個項目要結項了,但客戶要求 TPS 能達到上千,而用我寫的代碼再怎么弄成只能達到 30 + 的 TPS,然后我又將代碼中能緩存的都緩存了,能拆分的也都拆分了,拆分時用的線程池來實現的;其實現的代碼主要為以前寫的一篇博客中的實現方式來實現的。如下:

多線程之 futureTask(future,callable) 實例, jdbc 數據多線程查詢https://blog.csdn.net/puhaiyang/article/details/78041046

在其中用到了線程池,為了方便,線程池是采用如下代碼 new 出來的

final?ExecutorService?executorService?=?Executors.newFixedThreadPool(10);??

通過自己仔細想想后,感覺這代碼總有哪里寫得不對,為此特意寫了下 DEMO 代碼來,并用 jmeter 自己跑一下自己測下:

??@RequestMapping(value?=?"doTest")
????public?Object?doTest(@RequestParam(defaultValue?=?"false")?Boolean?shutdown,
?????????????????????????@RequestParam(defaultValue?=?"10")?Integer?threadCount,
?????????????????????????@RequestParam(defaultValue?=?"100")?Integer?sleepTime,
?????????????????????????@RequestParam(defaultValue?=?"10")?Integer?queryCount)?{
????????long?beginTime?=?System.currentTimeMillis();
????????final?ExecutorService?executorService?=?Executors.newFixedThreadPool(threadCount);
????????for?(int?i?=?0;?i?????????????int?finalI?=?i;
????????????Callable?callable?=?new?Callable()?{
????????????????@Override
????????????????public?Integer?call()?throws?Exception?{
????????????????????Thread.sleep(sleepTime);
????????????????????logger.debug("index:{}?threadInfo:{}",?finalI,?Thread.currentThread().toString());return?finalI;
????????????????}
????????????};
????????????FutureTask?futureTask?=?new?FutureTask(callable);
????????????executorService.submit(futureTask);
????????}if?(shutdown)?{
????????????executorService.shutdown();
????????}
????????Long?endTime?=?System.currentTimeMillis();
????????endTime?=?endTime?-?beginTime;
????????logger.info("info:{}",?endTime);return?atomicInteger.addAndGet(endTime.intValue())?+?"???this:"?+?endTime;
????}?

代碼如上所示,然后我用 jmeter 對此進行了測試,測試 1000 個請求去訪問,每個任務線程休眠時間設的為 1 秒,TPS 為 20 多。

一想這確實挺低的,然后分析其原因,想著是不是 springBoot 的線程數給的太少了,于是乎又把 tomcat 的最大線程數進行了修改,由默認的 200 修改為了 500,但發現沒啥大的變化,想了想后,可能問題不是 tomcat 的配置導致的。

server:
??tomcat:
????max-threads:?500?

然后又通過 Java VisualVM 工具看了看線程信息,沒發現啥問題。

然后出去靜了靜,聽了一兩首音樂后想著起來 Executors 還有一個 newCachedThreadPool() 的用法,它與 newFixedThreadPool() 的區別通過源碼可以大概知道個一二:

newFixedThreadPool:

????/**
?????*?Creates?a?thread?pool?that?reuses?a?fixed?number?of?threads
?????*?operating?off?a?shared?unbounded?queue.??At?any?point,?at?most
?????*?{@code?nThreads}?threads?will?be?active?processing?tasks.
?????*?If?additional?tasks?are?submitted?when?all?threads?are?active,
?????*?they?will?wait?in?the?queue?until?a?thread?is?available.
?????*?If?any?thread?terminates?due?to?a?failure?during?execution
?????*?prior?to?shutdown,?a?new?one?will?take?its?place?if?needed?to
?????*?execute?subsequent?tasks.??The?threads?in?the?pool?will?exist
?????*?until?it?is?explicitly?{@link?ExecutorService#shutdown?shutdown}.
?????*
?????*?@param?nThreads?the?number?of?threads?in?the?pool
?????*?@return?the?newly?created?thread?pool
?????*?@throws?IllegalArgumentException?if?{@code?nThreads?<=?0}
?????*/
????public?static?ExecutorService?newFixedThreadPool(int?nThreads)?{
????????return?new?ThreadPoolExecutor(nThreads,?nThreads,
??????????????????????????????????????0L,?TimeUnit.MILLISECONDS,
??????????????????????????????????????new?LinkedBlockingQueue());
????}

newCachedThreadPool:

????/**
?????*?Creates?a?thread?pool?that?creates?new?threads?as?needed,?but
?????*?will?reuse?previously?constructed?threads?when?they?are
?????*?available.??These?pools?will?typically?improve?the?performance
?????*?of?programs?that?execute?many?short-lived?asynchronous?tasks.
?????*?Calls?to?{@code?execute}?will?reuse?previously?constructed
?????*?threads?if?available.?If?no?existing?thread?is?available,?a?new
?????*?thread?will?be?created?and?added?to?the?pool.?Threads?that?have
?????*?not?been?used?for?sixty?seconds?are?terminated?and?removed?from
?????*?the?cache.?Thus,?a?pool?that?remains?idle?for?long?enough?will
?????*?not?consume?any?resources.?Note?that?pools?with?similar
?????*?properties?but?different?details?(for?example,?timeout?parameters)
?????*?may?be?created?using?{@link?ThreadPoolExecutor}?constructors.
?????*
?????*?@return?the?newly?created?thread?pool
?????*/
????public?static?ExecutorService?newCachedThreadPool()?{
????????return?new?ThreadPoolExecutor(0,?Integer.MAX_VALUE,
??????????????????????????????????????60L,?TimeUnit.SECONDS,
??????????????????????????????????????new?SynchronousQueue());
????}

newFixedThreadPool 是創建一個大小固定的線程池,線程數固定,也不會被回收

newCachedThreadPool 是創建一個大小為 MAX_VALUE 的線程數,并具有緩存功能,如果 60 秒內線程一直 處于空閑,則會進行回收

另外,線程池的 shutdown 方法 doc 文檔的解釋如下:

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down. This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

指的是等待線程池執行 shutdown 方法后就不再接收新的執行目標了,等當前線程池中的現場執行完畢后,此線程池就會關閉掉了。

通過查看 JAVA 源碼中的注釋信息后才得知原來我之前寫的代碼有了一個大大的 BUG,不應該執行完一次后就立即把線程池給 shutdown 掉,這樣的話,線程池的意義就沒多大的意思了,跟 new Thread 的就差不多了。尷尬了!

然后將測試代碼修改為如下的代碼:

import?org.slf4j.Logger;
import?org.slf4j.LoggerFactory;
import?org.springframework.web.bind.annotation.RequestMapping;
import?org.springframework.web.bind.annotation.RequestParam;
import?org.springframework.web.bind.annotation.RestController;
?
import?java.util.concurrent.Callable;
import?java.util.concurrent.ExecutorService;
import?java.util.concurrent.Executors;
import?java.util.concurrent.FutureTask;
import?java.util.concurrent.atomic.AtomicInteger;
?
@RestController
@RequestMapping(value?=?"test")
public?class?TestController?{
????private?Logger?logger?=?LoggerFactory.getLogger(this.getClass());
?
????private?AtomicInteger?atomicInteger?=?new?AtomicInteger(0);
????final?ExecutorService?executorService?=?Executors.newCachedThreadPool();
?
????@RequestMapping(value?=?"doTest")
????public?Object?doTest(@RequestParam(defaultValue?=?"false")?Boolean?shutdown,
?????????????????????????@RequestParam(defaultValue?=?"10")?Integer?threadCount,
?????????????????????????@RequestParam(defaultValue?=?"100")?Integer?sleepTime,
?????????????????????????@RequestParam(defaultValue?=?"10")?Integer?queryCount)?{
????????long?beginTime?=?System.currentTimeMillis();
//????????final?ExecutorService?executorService?=?Executors.newFixedThreadPool(threadCount);
????????for?(int?i?=?0;?i?????????????int?finalI?=?i;
????????????Callable?callable?=?new?Callable()?{
????????????????@Override
????????????????public?Integer?call()?throws?Exception?{
????????????????????Thread.sleep(sleepTime);
????????????????????logger.debug("index:{}?threadInfo:{}",?finalI,?Thread.currentThread().toString());return?finalI;
????????????????}
????????????};
????????????FutureTask?futureTask?=?new?FutureTask(callable);
????????????executorService.submit(futureTask);
????????}if?(shutdown)?{
????????????executorService.shutdown();
????????}
????????Long?endTime?=?System.currentTimeMillis();
????????endTime?=?endTime?-?beginTime;
????????logger.info("info:{}",?endTime);return?atomicInteger.addAndGet(endTime.intValue())?+?"???this:"?+?endTime;
????}
}

調用時,shutdown 傳入 false,并且線程池的 new 方法放到上面的公共方法區域中,而不應該是來一個請求就 new 一個線程池出來。然后將同樣的請求用 jmeter 測試后,發現能達到 300 多了,比之前的 20 多提升了許多倍!

總結:

通過上面的測試,發現了一個我之前用錯的 JAVA 線程池的用法,通過 jmeter 工具測試后才知道其性能是如何的大。

同時在通過修改 springboot 的配置信息后,發現 springBoot 能創建的線程池最大線程數也與其 tomcat 的最大線程數有關,具體身體關系還得靠后面的慢慢探索了。(賊尷尬,這么基礎的代碼問題居然給犯下了這么大一個錯誤. 還好及時地修改了。哈哈)


作者:水中加點糖

來源鏈接:

https://blog.csdn.net/puhaiyang/article/details/80530495

695150a34d934f7e08811417da81691d.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379132.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379132.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379132.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

引以為鑒-ARM開發板連線注意事項

前些日子把實驗室的三臺機子放到一個工位上&#xff0c;非常擁擠&#xff0c;做實驗也很不方便。因此&#xff0c;想把ARM開發板的環境重新搭建到自己的電腦上。說完就做&#xff0c;上午就開始忙活起來。把開發板上的USB線、串口線、JTAT接口、還有電源線一一插好。接著就開始…

CString 類型和引用

怎么理解CString & 類型&#xff1f;在函數參數表中&#xff0c;列了一項是此類型&#xff0c;據說是引用。可以給個具體方法&#xff0c;示例么&#xff1f; 由于子程序調用是棧傳遞參數&#xff0c;因此對參數的修改不會改變調用者傳入的參數的值。如果要改變調用者的參數…

Java IdentityHashMap putAll()方法與示例

IdentityHashMap類putAll()方法 (IdentityHashMap Class putAll() method) putAll() method is available in java.util package. putAll()方法在java.util包中可用。 putAll() method is used to copy all of the entry (key-value pairs) that exists from the given map (m)…

Python---實驗八

1&#xff0c;現在有一份‘邀請函.txt’的空白文件&#xff0c;請在同級目錄下編寫一段代碼&#xff0c;寫入內容‘誠摯邀請您來參加本次宴會’。 with open(fG:\study\Python\邀請函.txt,modew,encodingutf-8) as y:y.write(誠摯邀請您來參加本次宴會)效果圖如下&#xff1a;…

哈希表 - (代碼、分析 )

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;代碼&#xff1a; BSTree.h BSTree.c 二叉排序樹(Binary Sort Tree) 又稱為二叉查找樹(Binary Search Tree) Hash.h #ifndef _HASH_H_ #define _HASH_H_typedef void Hash;//定義哈希表類型 typedef void HashKey;//定義哈…

scala spark 數據對比_IT大牛耗時三個月總結出大數據領域學習路線,網友評論:炸鍋了...

大數據不是某個專業或一門編程語言&#xff0c;實際上它是一系列技術的組合運用。有人通過下方的等式給出了大數據的定義。大數據 編程技巧 數據結構和算法 分析能力 數據庫技能 數學 機器學習 NLP OS 密碼學 并行編程雖然這個等式看起來很長&#xff0c;需要學習的東…

Java IdentityHashMap equals()方法與示例

IdentityHashMap類equals()方法 (IdentityHashMap Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this IdentityHashMap object and the given object (ob) are eq…

jQuery中關于Ajax的詳解

本文介紹如何使用jquery實現Ajax功能. 用于發送Ajax請求的相關函數如load, get, getJSON和post這些漸變Ajax方法, 對于核心的ajax 方法沒有過多介紹, 主要是通過配置復雜的參數實現完全控制Ajax請求。 Ajax讓用戶頁面豐富起來, 增強用戶體驗. Ajax是所有Web開發的必修課. 雖然A…

Python---實驗九作業

1&#xff0c;使用tkinter實現計算器程序。實現效果如下&#xff1a; from tkinter import * from tkinter.ttk import *def frame(master):"""將共同的屬性作為默認值, 以簡化Frame創建過程"""w Frame(master)w.pack(sideTOP, expandYES, fill…

分析FLV文件分析和解析器的開源代碼

分析一下GitHub上一份FLV文件分析和解析器的開源代碼 GitHub源碼地址&#xff1a;功能強大的 FLV 文件分析和解析器 &#xff1a;可以將flv文件的視頻tag中的h264類型數據和音頻tag中的aac類型數據導出 &#xff08;只限h264和aac&#xff09; (這個代碼不太適合用于大文件的分…

用pv操作描述如下前驅圖_LinkedList實現分析(二)——常用操作

上一篇文章LinkedList實現分析(一)——LinkedList初探與對象創建介紹了LinkedList中的一些重要屬性和構造方法&#xff0c;下面我們將詳細介紹一下LinkedList提高的常用方法的實現原理元素添加###add(E e)方法往LinkedList添加元素&#xff0c;LinkedList提供了多重方式&#x…

C++多重繼承與虛基類及與.NET的比較

多重繼承前面我們介紹的派生類只有一個基類&#xff0c;稱為單基派生或單一繼承。在實際運用中&#xff0c;我們經常需要派生類同時具有多個基類&#xff0c;這種方法稱為多基派生或多重繼承。2.1 多重繼承的聲明&#xff1a;在 C 中&#xff0c;聲明具有兩個以上基類的派生類與…

Javascript的IE和Firefox兼容性匯編

window.event現有問題&#xff1a;使用 window.event 無法在 FF 上運行解決方法&#xff1a;FF 的 event 只能在事件發生的現場使用&#xff0c;此問題暫無法解決。可以這樣變通&#xff1a;原代碼(可在IE中運行)&#xff1a;<input type"button" name"someB…

Java Double類compareTo()方法與示例

雙類compareTo()方法 (Double class compareTo() method) compareTo() method is available in java.lang package. compareTo()方法在java.lang包中可用。 compareTo() method is used to check equality or inequality for this Double-object against the given Double-obje…

平院實訓門禁系統導入

這是我的配置&#xff08;如果是Win10最好每一步都管理員身份運行&#xff09; win7 SQLServer2008 VS2012 切記&#xff1a;注意&#xff1a;當你SQLserver創建數據庫和VS連接數據庫的時候得用同一種方式&#xff0c;要么都用window&#xff08;主機名&#xff09;&#xff0…

ffmpeg 解碼音頻(aac、mp3)輸出pcm文件

ffmpeg 解碼音頻&#xff08;aac、mp3&#xff09;輸出pcm文件 播放pcm可以參考&#xff1a; ffplay -ar 48000 -ac 2 -f f32le out.pcm main.c #include <stdio.h> #include <stdlib.h> #include <string.h>#include <libavutil/frame.h> #include …

Jquery getJSON()

getJSON與aspx 準備工作 Customer類 public class Customer{public int Unid { get; set; }public string CustomerName { get; set; }public string Memo { get; set; }public string Other { get; set; }}&#xff08;一&#xff09;ashx Customer customer new Customer { …

北京中信銀行總行地址_中信銀行拉薩分行舉行“存款保險標識”啟用和存款保險條例宣傳活動...

11月NOV中信銀行拉薩分行舉行“存款保險標識”啟用和《存款保險條例》宣傳活動揭牌啟用儀式111月Jul根據人民銀行和總行關于“存款保險標識”啟用工作相關要求&#xff0c;分行行領導高度重視“存款保險標識”啟用和《存款保險條例》宣傳活動工作&#xff0c;按照統一工作部署、…

Java ClassLoader getPackage()方法與示例

ClassLoader類的getPackage()方法 (ClassLoader Class getPackage() method) getPackage() method is available in java.lang package. getPackage()方法在java.lang包中可用。 getPackage() method is used to return the package that has been defined in ClassLoader or t…

C---編寫程序:求出1~1000之間能被7或12整除,但不能同時被二者整除的所有整數,將結果保存在數組中,要求程序數據的輸入、計算和輸出均使用函數實現。

編寫程序&#xff1a;求出1~1000之間能被7或12整除&#xff0c;但不能同時被二者整除的所有整數&#xff0c;將結果保存在數組中&#xff0c;要求程序數據的輸入、計算和輸出均使用函數實現。 編程思路&#xff1a;分別編寫函數input()、cal()、output()實現數據的輸入、計算和…