{"id":16783,"date":"2024-12-17T10:39:54","date_gmt":"2024-12-17T02:39:54","guid":{"rendered":"https:\/\/92it.top\/?p=16783"},"modified":"2024-12-17T10:40:08","modified_gmt":"2024-12-17T02:40:08","slug":"%e5%b9%b6%e5%8f%91%e5%92%8c%e5%b9%b6%e8%a1%8c%ef%bc%8c%e5%a6%82%e4%bd%95%e5%8c%ba%e5%88%86%ef%bc%9f%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16783","title":{"rendered":"\u5e76\u53d1\u548c\u5e76\u884c\uff0c\u5982\u4f55\u533a\u5206\uff1f\u5982\u4f55\u4f7f\u7528\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"http:\/\/\u5e76\u53d1\u548c\u5e76\u884c\uff0c\u5982\u4f55\u533a\u5206\uff1f\u5982\u4f55\u4f7f\u7528\uff1f\">\u5e76\u53d1\u548c\u5e76\u884c\uff0c\u5982\u4f55\u533a\u5206\uff1f\u5982\u4f55\u4f7f\u7528\uff1f<\/a><\/p>\n\n\n\n<p>\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u201c\u5e76\u53d1\u201d\u548c\u201c\u5e76\u884c\u6027\u201d\u662f\u4e24\u4e2a\u7ecf\u5e38\u88ab\u6df7\u6dc6\u4f46\u5b9e\u9645\u4e0a\u5177\u6709\u4e0d\u540c\u542b\u4e49\u7684\u6982\u5ff5\u3002\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e24\u4e2a\u6982\u5ff5\uff0c\u5e76\u901a\u8fc7Java\u4ee3\u7801\u6f14\u793a\u5b83\u4eec\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u5173\u4e8e\u5e76\u884c\u548c\u5e76\u53d1\uff0c \u5148\u770b\u4e00\u5f20\u5f88\u5f62\u8c61\u7684 gif\u56fe\u7247\uff08\u56fe\u7247\u6765\u81ea\u7f51\u7edc\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/640.gif\" alt=\"\" class=\"wp-image-16784\"\/><\/figure>\n\n\n\n<p>\u63a5\u7740\uff0c\u6211\u4eec\u5bf9\u7167\u8fd9\u4e0a\u9762\u7684gif\u56fe\u6765\u8be6\u7ec6\u5730\u5206\u6790\u4e24\u8005\u3002<\/p>\n\n\n\n<p><strong>1. \u5e76\u53d1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1.1 \u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p><strong>\u5e76\u53d1\u6027\uff08Concurrency\uff09<\/strong>\u662f\u6307\u7cfb\u7edf\u80fd\u591f\u5904\u7406\u591a\u4e2a\u4efb\u52a1\uff0c\u4f46\u4e0d\u4e00\u5b9a\u662f\u540c\u65f6\u6267\u884c\u3002\u5173\u952e\u5728\u4e8e\u4efb\u52a1\u7684\u7ba1\u7406\uff0c\u4f7f\u5f97\u591a\u4e2a\u4efb\u52a1\u5728\u65f6\u95f4\u4e0a\u4ea4\u9519\u8fdb\u884c\uff0c\u4ee5\u63d0\u9ad8\u8d44\u6e90\u5229\u7528\u7387\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u56fe\uff0c\u5728\u4e00\u4e2a CPU\u4e0a\uff0c\u4ea4\u66ff\u6267\u884c\u591a\u4e2atask\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"293\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-1024x293.png\" alt=\"\" class=\"wp-image-16785\" style=\"width:510px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-1024x293.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-300x86.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-768x220.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-830x238.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-230x66.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-350x100.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5-480x137.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-5.png 1334w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>1.2 \u7279\u70b9<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u4efb\u52a1\u5207\u6362<\/strong>\uff1a\u5728\u5355\u6838\u6216\u591a\u6838\u7cfb\u7edf\u4e0a\uff0c\u901a\u8fc7\u5feb\u901f\u5207\u6362\u4efb\u52a1\uff0c\u8ba9\u7528\u6237\u611f\u89c9\u4efb\u52a1\u662f\u540c\u65f6\u8fdb\u884c\u7684\u3002<\/li>\n\n\n\n<li><strong>\u8d44\u6e90\u5171\u4eab<\/strong>\uff1a\u591a\u4e2a\u4efb\u52a1\u5171\u4eab\u7cfb\u7edf\u8d44\u6e90\uff0c\u5982CPU\u3001\u5185\u5b58\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65\u5904\u7406<\/strong>\uff1a\u4efb\u52a1\u53ef\u4ee5\u5728\u7b49\u5f85\u67d0\u4e9b\u64cd\u4f5c\u5b8c\u6210\uff08\u5982I\/O\uff09\u65f6\uff0c\u5207\u6362\u5230\u5176\u4ed6\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>1.3 \u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u7528\u6237\u754c\u9762<\/strong>\uff1a\u5728\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u8d1f\u8d23\u54cd\u5e94\u7528\u6237\u8f93\u5165\uff0c\u800c\u540e\u53f0\u7ebf\u7a0b\u5904\u7406\u8017\u65f6\u64cd\u4f5c\uff0c\u4f7f\u754c\u9762\u4fdd\u6301\u54cd\u5e94\u3002<\/li>\n\n\n\n<li><strong>\u670d\u52a1\u5668\u5904\u7406<\/strong>\uff1aWeb\u670d\u52a1\u5668\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u901a\u8fc7\u7ebf\u7a0b\u6c60\u6216\u5f02\u6b65IO\u7ba1\u7406\u5e76\u53d1\u8fde\u63a5\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>1.4 Java\u5b9e\u73b0\u793a\u4f8b<\/strong><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Java\u5e76\u53d1\u793a\u4f8b\uff0c\u6a21\u62df\u591a\u4e2a\u4efb\u52a1\u4ea4\u66ff\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class ConcurrencyExample {\n    public static void main(String[] args) {\n        Runnable task1 = () -> {\n            for(int i=1; i&lt;=5; i++) {\n                System.out.println(\"Task1 - Count: \" + i);\n                try { Thread.sleep(100); } catch (InterruptedException e) {}\n            }\n        };\n\n        Runnable task2 = () -> {\n            for(int i=1; i&lt;=5; i++) {\n                System.out.println(\"Task2 - Count: \" + i);\n                try { Thread.sleep(100); } catch (InterruptedException e) {}\n            }\n        };\n\n        Thread thread1 = new Thread(task1);\n        Thread thread2 = new Thread(task2);\n\n        thread1.start();\n        thread2.start();\n    }\n}<\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\u793a\u4f8b<\/strong>\uff08\u4efb\u52a1\u4ea4\u9519\u6267\u884c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Task1 - Count: 1\nTask2 - Count: 1\nTask1 - Count: 2\nTask2 - Count: 2\n...<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a\u4efb\u52a1\u5728\u540c\u4e00\u4e2a\u5904\u7406\u5668\u4e0a\u4ea4\u66ff\u6267\u884c\uff0c\u5b9e\u73b0\u4e86\u5e76\u53d1\u6027\u3002<\/p>\n\n\n\n<p><strong>2. \u5e76\u884c\u6027<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>2.1 \u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p><strong>\u5e76\u884c\u6027\uff08Parallelism\uff09<\/strong>\u662f\u6307\u5229\u7528\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7cfb\u7edf\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u6216\u4efb\u52a1\u7684\u591a\u4e2a\u90e8\u5206\uff0c\u4ee5\u52a0\u5feb\u603b\u4f53\u5904\u7406\u901f\u5ea6\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u56fe\uff0c\u591a\u4e2aCPU\uff0c\u6bcf\u4e2aCPU\u4e0a\u5206\u522b\u6267\u884c\u4e00\u4e2a task\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"360\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-1024x360.png\" alt=\"\" class=\"wp-image-16786\" style=\"width:570px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-1024x360.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-300x106.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-768x270.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-830x292.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-230x81.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-350x123.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6-480x169.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/12\/image-6.png 1290w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>2.2 \u7279\u70b9<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u771f\u5b9e\u7684\u540c\u65f6\u6267\u884c<\/strong>\uff1a\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\uff0c\u591a\u4e2a\u4efb\u52a1\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u6838\u5fc3\u4e0a\u540c\u65f6\u8fd0\u884c\u3002<\/li>\n\n\n\n<li><strong>\u4efb\u52a1\u5206\u89e3<\/strong>\uff1a\u5927\u7684\u4efb\u52a1\u53ef\u4ee5\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u4efb\u52a1\uff0c\u5e76\u884c\u5904\u7406\u540e\u5408\u5e76\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u63d0\u5347<\/strong>\uff1a\u901a\u8fc7\u5e76\u884c\u6267\u884c\uff0c\u80fd\u591f\u663e\u8457\u7f29\u77ed\u5904\u7406\u65f6\u95f4\uff0c\u5c24\u5176\u9002\u5408\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2.3 \u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u79d1\u5b66\u8ba1\u7b97<\/strong>\uff1a\u6570\u503c\u6a21\u62df\u3001\u5929\u6c14\u9884\u62a5\u7b49\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u5927\u6570\u636e\u5904\u7406<\/strong>\uff1aHadoop\u3001Spark\u7b49\u6846\u67b6\u901a\u8fc7\u5e76\u884c\u8ba1\u7b97\u63d0\u9ad8\u6570\u636e\u5904\u7406\u901f\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2.4 Java\u5b9e\u73b0\u793a\u4f8b<\/strong><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Java\u5e76\u884c\u6d41\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u7684\u793a\u4f8b\uff0c\u8ba1\u7b971\u52301000000\u7684\u5e73\u65b9\u548c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.stream.LongStream;\n\npublic class ParallelismExample {\n    public static void main(String[] args) {\n        long startTime = System.currentTimeMillis();\n\n        long sum = LongStream.rangeClosed(1, 1_000_000)\n                             .parallel()\n                             .map(x -> x * x)\n                             .sum();\n\n        long endTime = System.currentTimeMillis();\n        System.out.println(\"Sum: \" + sum);\n        System.out.println(\"Time taken: \" + (endTime - startTime) + \" ms\");\n    }\n}<\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Sum: 333333833333500000\nTime taken: 50 ms<\/pre>\n\n\n\n<p>\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\uff0c<code>.parallel()<\/code>\u65b9\u6cd5\u4f7f\u5f97\u6d41\u64cd\u4f5c\u5e76\u884c\u6267\u884c\uff0c\u4ece\u800c\u52a0\u5feb\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n\n\n\n<p><strong>3. \u5e76\u53d1\u4e0e\u5e76\u884c\u6027\u7684\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>3.1 \u76ee\u6807<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u5e76\u53d1\u6027<\/strong>\uff1a\u4f7f\u591a\u4e2a\u4efb\u52a1\u6709\u6761\u4e0d\u7d0a\u5730\u8fdb\u884c\uff0c\u589e\u5f3a\u7cfb\u7edf\u54cd\u5e94\u80fd\u529b\u548c\u8d44\u6e90\u5229\u7528\u7387\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u884c\u6027<\/strong>\uff1a\u901a\u8fc7\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u63d0\u5347\u603b\u4f53\u5904\u7406\u901f\u5ea6\u548c\u541e\u5410\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>3.2 \u793a\u4f8b\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u5e76\u53d1<\/strong>\uff1a\u5355\u6838\u5904\u7406\u5668\u4e0a\uff0c\u901a\u8fc7\u65f6\u95f4\u7247\u8f6e\u8f6c\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u4f7f\u7528\u6237\u611f\u89c9\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u8fdb\u884c\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u884c<\/strong>\uff1a\u591a\u6838\u5904\u7406\u5668\u4e0a\uff0c\u591a\u4e2a\u4efb\u52a1\u6216\u4efb\u52a1\u7684\u90e8\u5206\u5728\u4e0d\u540c\u6838\u5fc3\u4e0a\u540c\u65f6\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>3.3 \u6027\u80fd\u8003\u8651<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u5e76\u53d1\u6027<\/strong>\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u5e94\u7528\uff0c\u901a\u8fc7\u7ba1\u7406\u4efb\u52a1\u7b49\u5f85\u65f6\u95f4\u63d0\u9ad8\u7cfb\u7edf\u6548\u7387\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u884c\u6027<\/strong>\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u5e94\u7528\uff0c\u901a\u8fc7\u5229\u7528\u591a\u6838\u8d44\u6e90\u52a0\u5feb\u8ba1\u7b97\u901f\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>3.4 \u8d44\u6e90\u5229\u7528<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u5e76\u53d1<\/strong>\u66f4\u5173\u6ce8\u4efb\u52a1\u7684\u8c03\u5ea6\u548c\u8d44\u6e90\u7684\u5171\u4eab\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u884c<\/strong>\u66f4\u5173\u6ce8\u5982\u4f55\u5212\u5206\u4efb\u52a1\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>4. \u901a\u8fc7\u5e76\u53d1\u548c\u5e76\u884c\u5b9e\u73b0\u7684Java\u6846\u67b6<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u5e76\u53d1\u548c\u5e76\u884c\u64cd\u4f5c\uff0c\u4e0b\u9762\u5206\u522b\u4e3e\u4e00\u4e2a\u4f8b\u5b50\u6765\u5c55\u793a\u5e76\u53d1\u548c\u5e76\u884c\u7684\u5b9e\u9645\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><strong>4.1 \u7ebf\u7a0b\u548cExecutor\u6846\u67b6<\/strong><\/p>\n\n\n\n<p><strong>\u7ebf\u7a0b<\/strong>\u662f\u5b9e\u73b0\u5e76\u53d1\u7684\u57fa\u672c\u5355\u5143\uff0cJava\u901a\u8fc7<code>Thread<\/code>\u7c7b\u548c<code>Runnable<\/code>\u63a5\u53e3\u63d0\u4f9b\u4e86\u5bf9\u7ebf\u7a0b\u7684\u652f\u6301\u3002\u4f46\u76f4\u63a5\u4f7f\u7528<code>Thread<\/code>\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u7ba1\u7406\u56f0\u96be\uff0c\u56e0\u6b64Java\u5f15\u5165\u4e86<code>Executor<\/code>\u6846\u67b6\uff0c\u7b80\u5316\u7ebf\u7a0b\u7ba1\u7406\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528ExecutorService\u5b9e\u73b0\u5e76\u53d1<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\n\npublic class ExecutorConcurrencyExample {\n    public static void main(String[] args) {\n        ExecutorService executor = Executors.newFixedThreadPool(2);\n\n        Runnable task1 = () -> {\n            for(int i=1; i&lt;=5; i++) {\n                System.out.println(\"Task1 - Count: \" + i);\n                try { Thread.sleep(100); } catch (InterruptedException e) {}\n            }\n        };\n\n        Runnable task2 = () -> {\n            for(int i=1; i&lt;=5; i++) {\n                System.out.println(\"Task2 - Count: \" + i);\n                try { Thread.sleep(100); } catch (InterruptedException e) {}\n            }\n        };\n\n        executor.submit(task1);\n        executor.submit(task2);\n\n        executor.shutdown();\n    }\n}<\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Task1 - Count: 1\nTask2 - Count: 1\nTask1 - Count: 2\nTask2 - Count: 2\n...<\/pre>\n\n\n\n<p><strong>4.2 \u5e76\u884c\u6d41\uff08Parallel Streams\uff09<\/strong><\/p>\n\n\n\n<p>Java 8\u5f15\u5165\u4e86<strong>Streams API<\/strong>\uff0c\u5b83\u652f\u6301\u987a\u5e8f\u548c\u5e76\u884c\u64cd\u4f5c\uff0c\u6781\u5927\u7b80\u5316\u4e86\u5e76\u884c\u5904\u7406\u7684\u7f16\u7a0b\u590d\u6742\u5ea6\u3002\u901a\u8fc7\u8c03\u7528<code>.parallel()<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u6d41\u64cd\u4f5c\u5e76\u884c\u5316\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a\u5e76\u884c\u5904\u7406\u5217\u8868<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.Arrays;\nimport java.util.List;\n\npublic class ParallelStreamExample {\n    public static void main(String[] args) {\n        List&lt;Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);\n\n        numbers.parallelStream()\n               .map(n -> {\n                   System.out.println(\"Processing \" + n + \" in \" + Thread.currentThread().getName());\n                   return n * n;\n               })\n               .forEach(result -> System.out.println(\"Result: \" + result));\n    }\n}<\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\u793a\u4f8b<\/strong>\uff08\u7ebf\u7a0b\u987a\u5e8f\u53ef\u80fd\u4e0d\u540c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Processing 2 in ForkJoinPool.commonPool-worker-1\nResult: 4\nProcessing 1 in ForkJoinPool.commonPool-worker-3\nResult: 1\n...<\/pre>\n\n\n\n<p><strong>5. \u5b9e\u8df5\u5efa\u8bae<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ol>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u5de5\u5177<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u7ebf\u7a0b\u7ba1\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ExecutorService<\/code>\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u4efb\u52a1\u8c03\u5ea6\uff0c\u8003\u8651\u4f7f\u7528<code>ForkJoinPool<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u5171\u4eab\u53ef\u53d8\u72b6\u6001<\/strong>\uff1a\u5171\u4eab\u72b6\u6001\u53ef\u80fd\u5bfc\u81f4\u7ade\u6001\u6761\u4ef6\uff08Race Conditions\uff09\uff0c\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\u6216\u540c\u6b65\u673a\u5236\u3002<\/li>\n\n\n\n<li><strong>\u7406\u89e3\u4efb\u52a1\u7684\u6027\u8d28<\/strong>\uff1aI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u9002\u5408\u5e76\u53d1\u5904\u7406\uff0cCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u9002\u5408\u5e76\u884c\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u5408\u7406\u5212\u5206\u4efb\u52a1<\/strong>\uff1a\u907f\u514d\u8fc7\u5ea6\u5212\u5206\u5bfc\u81f4\u7ebf\u7a0b\u5207\u6362\u5f00\u9500\u8fc7\u5927\uff0c\u6216\u4efb\u52a1\u7c92\u5ea6\u8fc7\u7c97\u5bfc\u81f4\u8d44\u6e90\u6d6a\u8d39\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u9ad8\u5c42\u6b21\u62bd\u8c61<\/strong>\uff1a\u5982Java 8\u7684<code>CompletableFuture<\/code>\uff0c\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>6. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u4ece\u591a\u4e2a\u7ef4\u5ea6\u5bf9\u6bd4\u4e86\u5e76\u53d1\u548c\u5e76\u884c\uff0c\u867d\u7136\u5728\u5904\u7406\u591a\u4efb\u52a1\u65b9\u9762\u5b83\u4eec\u6709\u5171\u540c\u4e4b\u5904\uff0c\u4f46\u5b83\u4eec\u7684\u76ee\u6807\u548c\u5b9e\u73b0\u65b9\u5f0f\u4e0d\u540c\u3002\u5e76\u53d1\u6027\u4fa7\u91cd\u4e8e\u4efb\u52a1\u7684\u7ba1\u7406\u548c\u8c03\u5ea6\uff0c\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u54cd\u5e94\u80fd\u529b\u548c\u8d44\u6e90\u5229\u7528\u7387\uff1b\u800c\u5e76\u884c\u6027\u5219\u4fa7\u91cd\u4e8e\u901a\u8fc7\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u6216\u4efb\u52a1\u7684\u591a\u4e2a\u90e8\u5206\uff0c\u4ee5\u63d0\u5347\u5904\u7406\u901f\u5ea6\u548c\u541e\u5410\u91cf\u3002<\/p>\n\n\n\n<ul>\n<li><strong>\u5e76\u53d1\u6027<\/strong>\u5173\u6ce8\u7684\u662f\u5982\u4f55\u7ed3\u6784\u5316\u7a0b\u5e8f\u4ee5\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u7684\u8fdb\u5c55\uff0c\u4e0d\u4e00\u5b9a\u540c\u65f6\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u884c\u6027<\/strong>\u5173\u6ce8\u7684\u662f\u5982\u4f55\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u4ee5\u52a0\u5feb\u603b\u4f53\u7684\u5904\u7406\u901f\u5ea6\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u5e76\u53d1\u548c\u5e76\u884c\uff0c\u5982\u4f55\u533a\u5206\uff1f\u5982\u4f55\u4f7f\u7528\uff1f \u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u201c\u5e76\u53d1\u201d\u548c\u201c\u5e76\u884c\u6027\u201d\u662f\u4e24\u4e2a\u7ecf\u5e38\u88ab\u6df7\u6dc6\u4f46\u5b9e\u9645\u4e0a\u5177\u6709\u4e0d\u540c\u542b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,30],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16783"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16783"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16783\/revisions"}],"predecessor-version":[{"id":16788,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16783\/revisions\/16788"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}