{"id":10020560,"date":"2024-12-28T15:30:02","date_gmt":"2024-12-28T07:30:02","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020560"},"modified":"2024-12-28T15:30:02","modified_gmt":"2024-12-28T07:30:02","slug":"%e4%bd%bf%e7%94%a8netty%e6%a1%86%e6%9e%b6%e4%b8%ad%e7%9a%84hashedwheeltimer%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%b9%b6%e5%8f%91%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020560","title":{"rendered":"\u4f7f\u7528Netty\u6846\u67b6\u4e2d\u7684HashedWheelTimer\u5b9e\u73b0\u9ad8\u5e76\u53d1\u5b9a\u65f6\u4efb\u52a1"},"content":{"rendered":"<h3>\u9879\u76ee\u76ee\u5f55\u7ed3\u6784<\/h3>\n<pre><code>HighConcurrencyTimerProject\/\n\u251c\u2500\u2500 src\/\n\u2502   \u251c\u2500\u2500 main\/\n\u2502   \u2502   \u251c\u2500\u2500 java\/\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 com\/\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 example\/\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 HighConcurrencyTimerExample.java\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 ThreadPoolExample.java\n\u2502   \u251c\u2500\u2500 test\/\n\u2502   \u2502   \u251c\u2500\u2500 java\/\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 com\/\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 example\/\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 HighConcurrencyTimerExampleTest.java\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 ThreadPoolExampleTest.java\n\u251c\u2500\u2500 pom.xmlHighConcurrencyTimerProject\/\n\u251c\u2500\u2500 src\/\n\u2502   \u251c\u2500\u2500 main\/\n\u2502   \u2502   \u251c\u2500\u2500 java\/\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 com\/\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 example\/\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 HighConcurrencyTimerExample.java\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 ThreadPoolExample.java\n\u2502   \u251c\u2500\u2500 test\/\n\u2502   \u2502   \u251c\u2500\u2500 java\/\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 com\/\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 example\/\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 HighConcurrencyTimerExampleTest.java\n\u2502   \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 ThreadPoolExampleTest.java\n\u251c\u2500\u2500 pom.xml<\/code><\/pre>\n<h3>\u4ee3\u7801\u6587\u4ef6<\/h3>\n<p><strong>HighConcurrencyTimerExample.java<\/strong><\/p>\n<pre><code>package com.example;\n\nimport io.netty.util.HashedWheelTimer;\nimport io.netty.util.Timeout;\nimport io.netty.util.TimerTask;\n\nimport java.util.concurrent.TimeUnit;\n\npublic class HighConcurrencyTimerExample {\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2aHashedWheelTimer\u5b9e\u4f8b\uff0c\u65f6\u95f4\u8f6e\u7684\u7cbe\u5ea6\u4e3a1\u6beb\u79d2\uff0c\u69fd\u6570\u4e3a1024\n        HashedWheelTimer timer = new HashedWheelTimer(1, TimeUnit.MILLISECONDS, 1024);\n\n        \/\/ \u521b\u5efa\u5e76\u6dfb\u52a0100\u4e07\u4e2a\u5b9a\u65f6\u4efb\u52a1\n        for (int i = 0; i &lt; 1000000; i++) {\n            timer.newTimeout(new TimerTask() {\n                @Override\n                public void run(Timeout timeout) {\n                    System.out.println(&quot;Task executed at: &quot; + new java.util.Date());\n                }\n            }, 1, TimeUnit.SECONDS);\n        }\n\n        \/\/ \u5173\u95ed\u5b9a\u65f6\u5668\n        Runtime.getRuntime().addShutdownHook(new Thread(timer::stop));\n    }\n}<\/code><\/pre>\n<p><strong>ThreadPoolExample.java<\/strong><\/p>\n<pre><code>package com.example;\n\nimport java.util.concurrent.Executors;\nimport java.util.concurrent.ScheduledExecutorService;\nimport java.util.concurrent.TimeUnit;\n\npublic class ThreadPoolExample {\n    public static void main(String[] args) {\n        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);\n        for (int i = 0; i &lt; 1000000; i++) {\n            executorService.schedule(() -&gt; System.out.println(&quot;Task executed at: &quot; + new java.util.Date()), 1, TimeUnit.SECONDS);\n        }\n        executorService.shutdown();\n    }\n}<\/code><\/pre>\n<p><strong>pom.xml<\/strong><\/p>\n<pre><code>&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot;\n         xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n         xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt;\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n    &lt;groupId&gt;com.example&lt;\/groupId&gt;\n    &lt;artifactId&gt;HighConcurrencyTimerProject&lt;\/artifactId&gt;\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\n    &lt;dependencies&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;io.netty&lt;\/groupId&gt;\n            &lt;artifactId&gt;netty-all&lt;\/artifactId&gt;\n            &lt;version&gt;4.1.68.Final&lt;\/version&gt;\n        &lt;\/dependency&gt;\n    &lt;\/dependencies&gt;\n&lt;\/project&gt;<\/code><\/pre>\n<h3>\u6784\u5efa\u9879\u76ee<\/h3>\n<ul>\n<li>\u6253\u5f00\u7ec8\u7aef\uff0c\u5bfc\u822a\u5230\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u6784\u5efa\u9879\u76ee\uff1a<\/li>\n<\/ul>\n<pre><code>mvn clean install<\/code><\/pre>\n<h3>\u8fd0\u884c\u9879\u76ee\uff1a<\/h3>\n<ul>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c HighConcurrencyTimerExample\uff1a<\/li>\n<\/ul>\n<pre><code>mvn exec:java -Dexec.mainClass=&quot;com.example.HighConcurrencyTimerExample&quot;<\/code><\/pre>\n<p>\u5f53\u4f60\u8fd0\u884c HighConcurrencyTimerExample \u65f6\uff0c\u63a7\u5236\u53f0\u4f1a\u6bcf\u79d2\u8f93\u51fa\u5927\u91cf\u4efb\u52a1\u6267\u884c\u7684\u65f6\u95f4\u6233\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>Task executed at: Fri Dec 27 22:10:00 CST 2024\nTask executed at: Fri Dec 27 22:10:00 CST 2024\nTask executed at: Fri Dec 27 22:10:00 CST 2024\n...<\/code><\/pre>\n<ul>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c ThreadPoolExample\uff1a<\/li>\n<\/ul>\n<pre><code>mvn exec:java -Dexec.mainClass=&quot;com.example.ThreadPoolExample&quot;<\/code><\/pre>\n<p>\u5f53\u4f60\u8fd0\u884c ThreadPoolExample \u65f6\uff0c\u63a7\u5236\u53f0\u4e5f\u4f1a\u6bcf\u79d2\u8f93\u51fa\u5927\u91cf\u4efb\u52a1\u6267\u884c\u7684\u65f6\u95f4\u6233\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>Task executed at: Fri Dec 27 22:10:00 CST 2024\nTask executed at: Fri Dec 27 22:10:00 CST 2024\nTask executed at: Fri Dec 27 22:10:00 CST 2024\n...<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045684813\">https:\/\/segmentfault.com\/a\/1190000045684813<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u76ee\u5f55\u7ed3\u6784 HighConcurrencyTimerProject\/ \u251c\u2500\u2500 src\/ \u2502 \u251c\u2500\u2500 main [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020560","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020560"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020560\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}