首页 > 其他分享 > 核心线程数和最大线程数的区别

核心线程数和最大线程数的区别

时间:2023-02-10 19:24:47浏览次数:36  
标签:web 区别 核心 springframework 线程 org import public

首先看下来自实测的效果图

 

 这里说明线程过多产生的影响和最大线程数与定义的队列长度有关

 

 以上就是核心线程数和最大线程数的区别

代码部分

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.web</groupId>
    <artifactId>web-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web-study</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--ThreadFactoryBuilder的依赖包,多线程使用-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
View Code
package com.web.webstudy.test;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2023/2/10 17:21
 */
public class CustomThreadFactory implements ThreadFactory {


    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    public CustomThreadFactory() {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() :
                Thread.currentThread().getThreadGroup();
        namePrefix = "线程池数量-" +
                poolNumber.getAndIncrement() +
                "-线程名称-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r,
                namePrefix + threadNumber.getAndIncrement() + "",
                0);
        if (t.isDaemon()){
            t.setDaemon(false);
        }
        if (t.getPriority() != Thread.NORM_PRIORITY){
            t.setPriority(Thread.NORM_PRIORITY);
        }

        return t;
    }

}

  

package com.web.webstudy;

import com.web.webstudy.test.CustomThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @Description: 自定义线程池
 * @Author: Yourheart
 * @Create: 2023/2/9 10:38
 */
@Component
public class ThreadPool {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        //核心线程数
        int corePoolSize = 2;
        //最大线程数
        int maximumPoolSize = 5;
        //超过 corePoolSize 线程数量的线程最大空闲时间
        long keepAliveTime = 0;
        //以秒为时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        //创建工作队列,用于存放提交的等待执行任务
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(10);
        ThreadFactory threadFactory = new CustomThreadFactory();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        return threadPoolExecutor;

    }

}

  

package com.web.webstudy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2023/2/9 10:44
 */
@RestController
@Slf4j
public class ThreadPoolController {

    @Autowired
    private  ThreadPoolExecutor threadPoolExecutor;

    @RequestMapping("/test")
    public String test(int threadPoolNum,int threadNum){

        for (int i=0;i<threadPoolNum;i++){

            for (int k=0;k<threadNum;k++){
                threadPoolExecutor.execute(new ThreadPoolTest(k));
            }
        }
        return "success";

    }
}

  

package com.web.webstudy;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2023/2/9 10:43
 */
@Slf4j
public class ThreadPoolTest implements Runnable {


    private int count;

    public ThreadPoolTest(int count) {
        this.count = count;
    }

    @Override
    public void run() {

        String name = Thread.currentThread().getName();
        log.info("线程名称:{},count:{}",name,count);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}

  

package com.web.webstudy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebStudyApplication.class, args);
    }

}

  我这边采用的接口调用的方式进行的测试的

接口地址 http://127.0.0.1:8080/test?threadPoolNum=1&threadNum=10

 

标签:web,区别,核心,springframework,线程,org,import,public
From: https://www.cnblogs.com/q202105271618/p/17108540.html

相关文章

  • PPTP和L2TP有哪些区别?
    PPTP和L2TP有哪些区别?1、PPTP要求互联网络为IP网络。L2TP只要求隧道媒介提供面向数据包的点对点的连接。L2TP可以在IP(使用UDP),帧中继永久虚拟电路(PVCs)、X.25虚拟电路(VCs)或AT......
  • 计算机的核心基础
    一、编程和编程语言二、计算机的本质三、计算机的五大组成部分四、计算机的三大硬件五、操作系统 一、编程和编程语言语言:语言其实就是人与人之间沟通的介质,如英语......
  • 抽象类和接口的区别
    1、抽象类是单继承,接口可以多实现。2、抽象类中方法的访问权限可以任意,接口里方法的访问权限只能是public3、抽象类中可以有构造方法,接口中不能有。4、抽象类中可以有......
  • 线程join 僵尸线程
    进程死亡后,由父进程负责回收PCB资源,不回收则会出现僵尸进程对于线程来说,任何一个线程都可以回收另一个线程的资源通常在主线程中通过pthread_join来回收子线程的资源调......
  • ChatGPT: delphi 实现生产者消费者队列处理多线程示例
    usesClasses,SyncObjs;typeTItem=classData:Integer;end;TProducer=class(TThread)privateFQueue:TThreadedQueue<TItem>;protect......
  • 干货:实时渲染和离线渲染的区别?实时云渲染又是什么?
    常见的渲染类型有以下几种:实时渲染、离线渲染、实时云渲染、混合渲染。那么什么是实时渲染?实时渲染和离线渲染有哪些区别?各自有哪些典型应用场景......有没有人感觉知道了......
  • Task线程问题
    引用自C#Task详解-五维思考-博客园(cnblogs.com)usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.CompilerServices;using......
  • Java多线程06——JUC并发包02
    1线程的同步工具类​​CountDownLatch​​​​CountDownLatch​​同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。​​CountDownLatch......
  • arthas内存也可以分析 -线程死锁 -cpu高
    https://blog.csdn.net/qq_43692950/article/details/122688520......
  • JQuery对象和JS对象区别与转换以及JQuery事件绑定,入口函数,样式控制
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>JQuery对象和js对象的转换</title><scriptsrc="js/jquery-3.3.1.min.js"></scri......