项目目录结构
HighConcurrencyTimerProject/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── HighConcurrencyTimerExample.java
│ │ │ │ │ ├── ThreadPoolExample.java
│ ├── test/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── HighConcurrencyTimerExampleTest.java
│ │ │ │ │ ├── ThreadPoolExampleTest.java
├── pom.xmlHighConcurrencyTimerProject/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── HighConcurrencyTimerExample.java
│ │ │ │ │ ├── ThreadPoolExample.java
│ ├── test/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── HighConcurrencyTimerExampleTest.java
│ │ │ │ │ ├── ThreadPoolExampleTest.java
├── pom.xml
代码文件
HighConcurrencyTimerExample.java
package com.example;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class HighConcurrencyTimerExample {
public static void main(String[] args) {
// 创建一个HashedWheelTimer实例,时间轮的精度为1毫秒,槽数为1024
HashedWheelTimer timer = new HashedWheelTimer(1, TimeUnit.MILLISECONDS, 1024);
// 创建并添加100万个定时任务
for (int i = 0; i < 1000000; i++) {
timer.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) {
System.out.println("Task executed at: " + new java.util.Date());
}
}, 1, TimeUnit.SECONDS);
}
// 关闭定时器
Runtime.getRuntime().addShutdownHook(new Thread(timer::stop));
}
}
ThreadPoolExample.java
package com.example;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadPoolExample {
public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
for (int i = 0; i < 1000000; i++) {
executorService.schedule(() -> System.out.println("Task executed at: " + new java.util.Date()), 1, TimeUnit.SECONDS);
}
executorService.shutdown();
}
}
pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>HighConcurrencyTimerProject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.68.Final</version>
</dependency>
</dependencies>
</project>
构建项目
- 打开终端,导航到项目根目录,运行以下命令来构建项目:
mvn clean install
运行项目:
- 使用以下命令运行 HighConcurrencyTimerExample:
mvn exec:java -Dexec.mainClass="com.example.HighConcurrencyTimerExample"
当你运行 HighConcurrencyTimerExample 时,控制台会每秒输出大量任务执行的时间戳。例如:
Task executed at: Fri Dec 27 22:10:00 CST 2024
Task executed at: Fri Dec 27 22:10:00 CST 2024
Task executed at: Fri Dec 27 22:10:00 CST 2024
...
- 使用以下命令运行 ThreadPoolExample:
mvn exec:java -Dexec.mainClass="com.example.ThreadPoolExample"
当你运行 ThreadPoolExample 时,控制台也会每秒输出大量任务执行的时间戳。例如:
Task executed at: Fri Dec 27 22:10:00 CST 2024
Task executed at: Fri Dec 27 22:10:00 CST 2024
Task executed at: Fri Dec 27 22:10:00 CST 2024
...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。