From 6aa044b16ee484ad98ce67c9d12c63d51d5ff6f8 Mon Sep 17 00:00:00 2001
From: Bunny <1319900154@qq.com>
Date: Wed, 22 Jan 2025 14:08:42 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BAMVC=E9=A1=B9=E7=9B=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/encodings.xml | 1 +
.idea/misc.xml | 1 +
.../thead/pool/FixedThreadPoolExample01.java | 27 ++++++++
...hed.java => ThreadPoolNewCached01Use.java} | 6 +-
.../ThreadPoolNewCached02ReuseExample.java | 47 ++++++++++++++
...PoolNewCached03CustomCachedThreadPool.java | 46 +++++++++++++
mvc/pom.xml | 64 +++++++++++++++++++
.../java/cn/bunny/mvc/MvcApplication.java | 13 ++++
mvc/src/main/resources/application.yaml | 3 +
.../cn/bunny/mvc/MvcApplicationTests.java | 13 ++++
pom.xml | 2 +-
11 files changed, 219 insertions(+), 4 deletions(-)
create mode 100644 multithreading/src/main/java/thead/pool/FixedThreadPoolExample01.java
rename multithreading/src/main/java/thead/pool/{ThreadPoolNewCached.java => ThreadPoolNewCached01Use.java} (84%)
create mode 100644 multithreading/src/main/java/thead/pool/ThreadPoolNewCached02ReuseExample.java
create mode 100644 multithreading/src/main/java/thead/pool/ThreadPoolNewCached03CustomCachedThreadPool.java
create mode 100644 mvc/pom.xml
create mode 100644 mvc/src/main/java/cn/bunny/mvc/MvcApplication.java
create mode 100644 mvc/src/main/resources/application.yaml
create mode 100644 mvc/src/test/java/cn/bunny/mvc/MvcApplicationTests.java
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
index f17d6f2..4b223af 100644
--- a/.idea/encodings.xml
+++ b/.idea/encodings.xml
@@ -8,6 +8,7 @@
+
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 2a0d8ae..dce4172 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -5,6 +5,7 @@
diff --git a/multithreading/src/main/java/thead/pool/FixedThreadPoolExample01.java b/multithreading/src/main/java/thead/pool/FixedThreadPoolExample01.java
new file mode 100644
index 0000000..f9fec81
--- /dev/null
+++ b/multithreading/src/main/java/thead/pool/FixedThreadPoolExample01.java
@@ -0,0 +1,27 @@
+package thead.pool;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class FixedThreadPoolExample01 {
+ public static void main(String[] args) {
+ // 创建一个固定大小的线程池,池中有 3 个线程
+ ExecutorService executor = Executors.newFixedThreadPool(3);
+
+ // 提交 6 个任务
+ for (int i = 0; i < 6; i++) {
+ final int taskId = i;
+ executor.submit(() -> {
+ System.out.println("Task " + taskId + " executed by " + Thread.currentThread().getName());
+ try {
+ Thread.sleep(1000); // 模拟任务执行
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ });
+ }
+
+ // 关闭线程池
+ executor.shutdown();
+ }
+}
diff --git a/multithreading/src/main/java/thead/pool/ThreadPoolNewCached.java b/multithreading/src/main/java/thead/pool/ThreadPoolNewCached01Use.java
similarity index 84%
rename from multithreading/src/main/java/thead/pool/ThreadPoolNewCached.java
rename to multithreading/src/main/java/thead/pool/ThreadPoolNewCached01Use.java
index f5a9391..ce74ff3 100644
--- a/multithreading/src/main/java/thead/pool/ThreadPoolNewCached.java
+++ b/multithreading/src/main/java/thead/pool/ThreadPoolNewCached01Use.java
@@ -3,14 +3,14 @@ package thead.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-public class ThreadPoolNewCached {
+public class ThreadPoolNewCached01Use {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
// 提交多个任务
- for (int i = 0; i < 10; i++) {
+ for (int i = 0; i < 1000; i++) {
final int taskId = i;
- executorService.submit(() -> {
+ executorService.execute(() -> {
System.out.println("Task " + taskId + " executed by: " + Thread.currentThread().getName());
try {
Thread.sleep(2000); // 模拟任务执行
diff --git a/multithreading/src/main/java/thead/pool/ThreadPoolNewCached02ReuseExample.java b/multithreading/src/main/java/thead/pool/ThreadPoolNewCached02ReuseExample.java
new file mode 100644
index 0000000..cf32aac
--- /dev/null
+++ b/multithreading/src/main/java/thead/pool/ThreadPoolNewCached02ReuseExample.java
@@ -0,0 +1,47 @@
+package thead.pool;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class ThreadPoolNewCached02ReuseExample {
+
+ public static void main(String[] args) throws InterruptedException {
+ // 创建一个缓存线程池
+ ExecutorService executorService = Executors.newCachedThreadPool();
+
+ // 提交多个任务
+ for (int i = 0; i < 5; i++) {
+ final int taskId = i;
+ executorService.submit(() -> {
+ System.out.println("Task " + taskId + " executed by: " + Thread.currentThread().getName());
+ try {
+ // 模拟任务执行时间
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ });
+ }
+
+ // 等待 1 秒钟,确保线程池中的线程已经执行了一些任务
+ Thread.sleep(1000);
+
+ // 再次提交一些任务,观察线程是否复用
+ for (int i = 5; i < 1000; i++) {
+ final int taskId = i;
+ executorService.submit(() -> {
+ System.out.println("Task " + taskId + " executed by: " + Thread.currentThread().getName());
+ try {
+ // 模拟任务执行时间
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ });
+ }
+
+ // 等待所有任务完成
+ Thread.sleep(5000);
+ executorService.shutdown();
+ }
+}
diff --git a/multithreading/src/main/java/thead/pool/ThreadPoolNewCached03CustomCachedThreadPool.java b/multithreading/src/main/java/thead/pool/ThreadPoolNewCached03CustomCachedThreadPool.java
new file mode 100644
index 0000000..76ca4ac
--- /dev/null
+++ b/multithreading/src/main/java/thead/pool/ThreadPoolNewCached03CustomCachedThreadPool.java
@@ -0,0 +1,46 @@
+package thead.pool;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+public class ThreadPoolNewCached03CustomCachedThreadPool {
+
+ public static void main(String[] args) {
+ // 定制化线程池
+ ThreadPoolExecutor executor = new ThreadPoolExecutor(
+ 0, // 核心线程数 0(不保持任何核心线程)
+ 5, // 最大线程数为 4
+ 60L, // 空闲线程保持时间 60 秒
+ TimeUnit.SECONDS, // 时间单位:秒
+ new SynchronousQueue<>(),// 使用一个无界队列(任务等待队列)
+ Executors.defaultThreadFactory(), // 使用默认线程工厂
+ new ThreadPoolExecutor.AbortPolicy() // 默认的拒绝策略:抛出异常
+ );
+
+ // 提交一些任务
+ for (int i = 0; i < 10; i++) {
+ final int taskId = i;
+ executor.submit(() -> {
+ System.out.println("Task " + taskId + " executed by: " + Thread.currentThread().getName());
+ try {
+ // 模拟任务执行时间
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ });
+ }
+
+ // 等待所有任务完成
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ // 关闭线程池
+ executor.shutdown();
+ }
+}
diff --git a/mvc/pom.xml b/mvc/pom.xml
new file mode 100644
index 0000000..22c6259
--- /dev/null
+++ b/mvc/pom.xml
@@ -0,0 +1,64 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 3.4.1
+
+
+ cn.bunny
+ mvc
+ 0.0.1-SNAPSHOT
+ mvc
+ mvc
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 17
+
+
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-devtools
+ runtime
+ true
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/mvc/src/main/java/cn/bunny/mvc/MvcApplication.java b/mvc/src/main/java/cn/bunny/mvc/MvcApplication.java
new file mode 100644
index 0000000..b393517
--- /dev/null
+++ b/mvc/src/main/java/cn/bunny/mvc/MvcApplication.java
@@ -0,0 +1,13 @@
+package cn.bunny.mvc;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class MvcApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(MvcApplication.class, args);
+ }
+
+}
diff --git a/mvc/src/main/resources/application.yaml b/mvc/src/main/resources/application.yaml
new file mode 100644
index 0000000..cd9fdcd
--- /dev/null
+++ b/mvc/src/main/resources/application.yaml
@@ -0,0 +1,3 @@
+spring:
+ application:
+ name: mvc
\ No newline at end of file
diff --git a/mvc/src/test/java/cn/bunny/mvc/MvcApplicationTests.java b/mvc/src/test/java/cn/bunny/mvc/MvcApplicationTests.java
new file mode 100644
index 0000000..087c3a5
--- /dev/null
+++ b/mvc/src/test/java/cn/bunny/mvc/MvcApplicationTests.java
@@ -0,0 +1,13 @@
+package cn.bunny.mvc;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class MvcApplicationTests {
+
+ @Test
+ void contextLoads() {
+ }
+
+}
diff --git a/pom.xml b/pom.xml
index b69d486..9ec1d25 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,9 +20,9 @@
- multithreading1
multithreading
local-tools
+ mvc