feat: 创建MVC项目
This commit is contained in:
parent
853b64a173
commit
6aa044b16e
|
@ -8,6 +8,7 @@
|
|||
<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-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/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources-filtered" charset="UTF-8" />
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/mvc/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="ignoredFiles">
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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); // 模拟任务执行
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
spring:
|
||||
application:
|
||||
name: mvc
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue