{"id":203265,"date":"2025-05-22T08:48:33","date_gmt":"2025-05-22T00:48:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203265\/"},"modified":"2025-05-22T08:48:33","modified_gmt":"2025-05-22T00:48:33","slug":"java-%e4%b8%ad%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%b9%b6%e5%8f%91","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203265\/","title":{"rendered":"Java \u4e2d\u7684\u7ebf\u7a0b\u5e76\u53d1"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u4e2d\u7684\u7ebf\u7a0b\u5e76\u53d1<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u4e2d\u7684\u7ebf\u7a0b\u5e76\u53d1\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241103\/173062606667274212b40d0.jpg\" class=\"aligncenter\"><\/p>\n<p>\u9ad8\u7ea7 java \u4e2d\u7684\u7ebf\u7a0b\u5e76\u53d1\u6216\u591a\u7ebf\u7a0b\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\uff0c\u4ece\u800c\u589e\u5f3a\u590d\u6742\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002\u4ee5\u4e0b\u662f\u5176\u5173\u952e\u6982\u5ff5\u548c\u5b9e\u7528\u7a0b\u5e8f\u7684\u7b80\u660e\u7ec6\u5206\u3002<\/p>\n<p>java \u4e2d\u591a\u7ebf\u7a0b\u7684\u4e3b\u8981\u7279\u6027\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u7ebf\u7a0b\u3002<\/li>\n<li>\u4f7f\u7528\u6267\u884c\u5668\u8fdb\u884c\u7ebf\u7a0b\u7ba1\u7406<\/li>\n<li>\u5e76\u53d1\u5b9e\u7528\u7a0b\u5e8f<\/li>\n<li>\u5206\u53c9\/\u8fde\u63a5\u6846\u67b6<\/li>\n<li>\u5177\u6709\u5b8c\u6574\u672a\u6765\u7684\u5f02\u6b65\u7f16\u7a0b<\/li>\n<\/ol>\n<p>1\ufe0f\u20e3 \u521b\u5efa\u8bdd\u9898\u3002<\/p>\n<ul>\n<li>\n<p>\u6269\u5c55\u7ebf\u7a0b\uff1a\u901a\u8fc7\u91cd\u5199 run() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0 runnable\uff1a\u5c06 runnable \u5b9e\u4f8b\u4f20\u9012\u7ed9 thread \u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0 callable\uff1a\u4e0e runnable \u4e0d\u540c\uff0ccallable \u5141\u8bb8\u7ebf\u7a0b\u8fd4\u56de\u7ed3\u679c\u5e76\u5904\u7406\u68c0\u67e5\u7684\u5f02\u5e38\u3002<\/p>\n<\/li>\n<\/ul>\n<p>2\ufe0f\u20e3 \u4f7f\u7528\u6267\u884c\u5668\u8fdb\u884c\u7ebf\u7a0b\u7ba1\u7406\u3002<\/p>\n<ul>\n<li>\n<p>java \u7684\u6267\u884c\u5668\u6846\u67b6 (java.util.concurrent.executorservice) \u7ba1\u7406\u7ebf\u7a0b\u6c60\uff0c\u5141\u8bb8\u9ad8\u6548\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p>fixedthreadpool \u548c cachedthreadpool \u7b49\u6267\u884c\u5668\u521b\u5efa\u4e00\u4e2a\u53ef\u91cd\u7528\u7ebf\u7a0b\u6c60\uff0c\u6709\u6548\u5730\u7ba1\u7406\u5b83\u4eec\u4ee5\u51cf\u5c11\u521b\u5efa\u65b0\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/p>\n<\/li>\n<\/ul>\n<p>3\ufe0f\u20e3 \u5e76\u53d1\u5b9e\u7528\u7a0b\u5e8f<\/p>\n<ul>\n<li>\n<p>\u9501\uff1a\u50cf reentrantlock \u8fd9\u6837\u7684\u9ad8\u7ea7\u9501\u5b9a\u673a\u5236\u6bd4\u540c\u6b65\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5b9a\u65f6\u548c\u53ef\u4e2d\u65ad\u7684\u9501\u5b9a\u3002<\/p>\n<\/li>\n<li>\n<p>\u539f\u5b50\u53d8\u91cf\uff1ajava.util.concurrent.atomic \u5305\u5305\u542b\u63d0\u4f9b\u65e0\u9501\u7ebf\u7a0b\u7684\u539f\u5b50\u7c7b\uff08atomicinteger\u3001atomiclong\uff09-<br \/> \u5b89\u5168\u8fd0\u8425\u3002<\/p>\n<\/li>\n<li>\n<p>\u540c\u6b65\u5668\uff1a\u5305\u62ec\u4ee5\u4e0b\u5b9e\u7528\u7a0b\u5e8f\uff1a<br \/> countdownlatch\uff1a\u5141\u8bb8\u7ebf\u7a0b\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u5b8c\u6210<br \/> \u4efb\u52a1\u3002<br \/> cyclicbarrier\uff1a\u5728\u516c\u5171<br \/>\u5904\u540c\u6b65\u56fa\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b \u969c\u788d\u70b9\u3002<br \/> \u4fe1\u53f7\u91cf\uff1a\u901a\u8fc7\u5141\u8bb8\u7279\u5b9a\u6570\u91cf\u6765\u63a7\u5236\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee<br \/> \u5e76\u53d1\u7ebf\u7a0b\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<p>4\ufe0f\u20e3 \u5206\u53c9\/\u8fde\u63a5\u6846\u67b6<\/p>\n<ul>\n<li>1.\u5bf9\u4e8e\u5206\u6cbb\u4efb\u52a1\uff0cforkjoinpool \u5c06\u4efb\u52a1\u62c6\u5206\u4e3a\u5e76\u884c\u5904\u7406\u7684\u8f83\u5c0f\u5b50\u4efb\u52a1\uff0c\u8fd9\u5728\u9012\u5f52\u7b97\u6cd5\u4e2d\u7279\u522b\u6709\u7528\u3002<\/li>\n<\/ul>\n<p>5\ufe0f\u20e3 \u5177\u6709\u5b8c\u6574\u672a\u6765\u7684\u5f02\u6b65\u7f16\u7a0b<\/p>\n<ul>\n<li>completablefuture \u652f\u6301\u5f02\u6b65\u548c\u975e\u963b\u585e\u7f16\u7a0b\uff0c\u5141\u8bb8\u94fe\u63a5\u548c\u7ec4\u5408\u590d\u6742\u5de5\u4f5c\u6d41\u7a0b\u7684\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u7ebf\u7a0b\u793a\u4f8b<\/strong><\/p>\n<p>\u4e3b\u7c7b\u8c03\u75282\u4e2a\u4e0d\u540c\u7684\u7ebf\u7a0b<\/p>\n<pre>public class threadconcurrence {\n    public static void main(string[] args) {\n        \/\/ there is 2 type you have to call thread method\n                \/\/1- extend thread class\n                \/\/1- implements runnable class\n        \/\/ why implement concept is introduce here\n                \/\/ because in java multiple thread dose not support that's so why implement class will introduce\n                \/\/ ex- when u extend (inherit) base call, then at that time this call can not extend another thread class.\n        int n = 10;\n        for (int i = 0; i &lt; n; i++) {\n\n            \/\/ in case of extend(inherit) thread class\n            thread1 t1 = new thread1();\n            t1.start();\n\n            \/\/ in case of implement runnable class\n            thread t2 =new thread(new thread2());\n            t2.start();\n        }\n    }\n}\n\n<\/pre>\n<p>\u7ebf\u7a0b1&#8211;\uff08\u6269\u5c55\u7ebf\u7a0b\uff09<\/p>\n<pre>public class thread1 extends thread{\n    \/\/if you are extend thread class then you  most be used run()\n    \/\/ because when you start a thread then run() automatically call and run\n    public void run(){\n        try {\n            system.out.println(\"thread1 is running now....\");\n        } catch (exception e) {\n            throw new runtimeexception(e);\n        }\n    }\n}\n\n<\/pre>\n<p>thread2&#8211;\uff08\u5b9e\u73b0 runnable\uff09<\/p>\n<pre>public class Thread2 implements Runnable {\n    \/\/IF you are implement thread Then run() will be executed.\n    \/\/ Because when you start a thread then run() automatically call and run\n    public void run(){\n        try {\n            System.out.println(\"Thread2 is running.......\");\n        } catch (Exception e) {\n            throw new RuntimeException(e);\n        }\n    }\n}\n\n<\/pre>\n<p>\u7ed3\u8bba\uff1a<\/p>\n<p>\u901a\u8fc7\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6846\u67b6\uff0c\u9ad8\u7ea7 java \u591a\u7ebf\u7a0b\u53ef\u4ee5\u6784\u5efa\u53ef\u65e0\u7f1d\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u7684\u53ef\u6269\u5c55\u3001\u9ad8\u6027\u80fd\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5982\u9700\u66f4\u591a\u89c1\u89e3\uff0c\u8bf7\u968f\u65f6\u63d0\u53ca\u60a8\u7684 linkedin \u548c github \u4ee5\u83b7\u53d6\u6df1\u5165\u7684\u793a\u4f8b\u548c\u4ee3\u7801\u793a\u4f8b\uff01\u5982\u679c\u60a8\u9700\u8981\u4efb\u4f55\u5177\u4f53\u8c03\u6574\uff0c\u8bf7\u544a\u8bc9\u6211\u3002<\/p>\n<p>linkedin\uff1ahttps:\/\/www.linkedin.com\/in\/pravanjan-17p\/<\/p>\n<p>github\uff1ahttps:\/\/github.com\/prabhanjan-17p<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aJava \u4e2d\u7684\u7ebf\u7a0b\u5e76\u53d1\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u4e2d\u7684\u7ebf\u7a0b\u5e76\u53d1 \u6536\u85cf \u5b66&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203265","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203265"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203265\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}