feat: 创建MVC项目

This commit is contained in:
Bunny 2025-01-22 14:08:42 +08:00
parent 853b64a173
commit 6aa044b16e
11 changed files with 219 additions and 4 deletions

View File

@ -8,6 +8,7 @@
<file url="file://$PROJECT_DIR$/multithreading1/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/multithreading1/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/multithreading1/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/multithreading1/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/multithreading1/src/main/resources-filtered" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/multithreading1/src/main/resources-filtered" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/mvc/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources-filtered" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/resources-filtered" charset="UTF-8" />

View File

@ -5,6 +5,7 @@
<option name="originalFiles"> <option name="originalFiles">
<list> <list>
<option value="$PROJECT_DIR$/pom.xml" /> <option value="$PROJECT_DIR$/pom.xml" />
<option value="$PROJECT_DIR$/mvc/pom.xml" />
</list> </list>
</option> </option>
<option name="ignoredFiles"> <option name="ignoredFiles">

View File

@ -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();
}
}

View File

@ -3,14 +3,14 @@ package thead.pool;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
public class ThreadPoolNewCached { public class ThreadPoolNewCached01Use {
public static void main(String[] args) { public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool(); ExecutorService executorService = Executors.newCachedThreadPool();
// 提交多个任务 // 提交多个任务
for (int i = 0; i < 10; i++) { for (int i = 0; i < 1000; i++) {
final int taskId = i; final int taskId = i;
executorService.submit(() -> { executorService.execute(() -> {
System.out.println("Task " + taskId + " executed by: " + Thread.currentThread().getName()); System.out.println("Task " + taskId + " executed by: " + Thread.currentThread().getName());
try { try {
Thread.sleep(2000); // 模拟任务执行 Thread.sleep(2000); // 模拟任务执行

View File

@ -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();
}
}

View File

@ -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();
}
}

64
mvc/pom.xml Normal file
View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>3.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.bunny</groupId>
<artifactId>mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mvc</name>
<description>mvc</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -0,0 +1,3 @@
spring:
application:
name: mvc

View File

@ -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() {
}
}

View File

@ -20,9 +20,9 @@
<developer/> <developer/>
</developers> </developers>
<modules> <modules>
<module>multithreading1</module>
<module>multithreading</module> <module>multithreading</module>
<module>local-tools</module> <module>local-tools</module>
<module>mvc</module>
</modules> </modules>
<scm> <scm>
<connection/> <connection/>