首先看下来自实测的效果图
这里说明线程过多产生的影响和最大线程数与定义的队列长度有关
以上就是核心线程数和最大线程数的区别
代码部分
<?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