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.mzph.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()实现数据的输入、计算和…