{"id":46396,"date":"2023-08-02T19:30:41","date_gmt":"2023-08-02T11:30:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=46396"},"modified":"2023-08-02T19:30:41","modified_gmt":"2023-08-02T11:30:41","slug":"java%e7%ba%bf%e7%a8%8b%e7%8a%b6%e6%80%81%ef%bc%9a5%e7%a7%8d%e8%bf%98%e6%98%af6%e7%a7%8d%e6%9c%89%e4%bd%95%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/46396.html","title":{"rendered":"Java\u7ebf\u7a0b\u72b6\u6001\uff1a5\u79cd\u8fd8\u662f6\u79cd\u6709\u4f55\u533a\u522b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<p>\u901a\u5e38\u60c5\u51b5\u4e0bJava\u7ebf\u7a0b\u67095\u79cd\u72b6\u6001\uff0c\u4f46\u6709\u65f6\u4e5f\u53ef\u4ee5\u5c06\u5176\u62c6\u5206\u4e3a6\u79cd\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5982\u4f55\u770b\u5f85\u7ebf\u7a0b\u5728TIMED_WAITING\u72b6\u6001\u4e0b\u7684\u884c\u4e3a\u3002<strong>1\u3001New\uff08\u65b0\u5efa\uff09\uff1a<\/strong> \u7ebf\u7a0b\u5bf9\u8c61\u88ab\u521b\u5efa\u4f46\u5c1a\u672a\u542f\u52a8\u65f6\u7684\u72b6\u6001\uff1b2\u3001<strong>Runnable\uff08\u8fd0\u884c\uff09\uff1a<\/strong> \u7ebf\u7a0b\u5bf9\u8c61\u8c03\u7528start()\u65b9\u6cd5\u540e\uff0c\u7ebf\u7a0b\u8fdb\u5165Runnable\u72b6\u6001\uff1b<strong>3\u3001Blocked\uff08\u963b\u585e\uff09\uff1a<\/strong> \u7ebf\u7a0b\u5728\u7b49\u5f85\u9501\u7684\u91ca\u653e\u65f6\u8fdb\u5165Blocked\u72b6\u6001\u7b49\u3002\u6709\u65f6\u5c06Timed Waiting\u72b6\u6001\u5355\u72ec\u5217\u4e3a\u4e00\u79cd\u72b6\u6001\uff0c\u662f\u4e3a\u4e86\u5f3a\u8c03\u7ebf\u7a0b\u5728\u6b64\u72b6\u6001\u4e0b\u5728\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u4f1a\u81ea\u52a8\u8fd4\u56deRunnable\u72b6\u6001\u3002<\/p>\n<\/blockquote>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"680\" height=\"421\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/08\/pingcode\u6b63\u6587-19.jpg?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\" class=\"wp-image-46398\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/08\/pingcode\u6b63\u6587-19.jpg 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/08\/pingcode\u6b63\u6587-19-300x186.jpg 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001<strong>Java\u7ebf\u7a0b\u72b6\u6001<\/strong><\/h2>\n\n\n\n<p>\u5728Java\u4e2d\uff0c\u7ebf\u7a0b\u5728\u4e0d\u540c\u7684\u6267\u884c\u9636\u6bb5\u4f1a\u6709\u4e0d\u540c\u7684\u72b6\u6001\uff0cJava\u5b9a\u4e49\u4e86\u51e0\u79cd\u7ebf\u7a0b\u72b6\u6001\u6765\u63cf\u8ff0\u7ebf\u7a0b\u5728\u4e0d\u540c\u9636\u6bb5\u7684\u72b6\u6001\u8f6c\u6362\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cJava\u7ebf\u7a0b\u67095\u79cd\u72b6\u6001\uff0c\u4f46\u6709\u65f6\u4e5f\u53ef\u4ee5\u5c06\u5176\u62c6\u5206\u4e3a6\u79cd\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5982\u4f55\u770b\u5f85\u7ebf\u7a0b\u5728TIMED_WAITING\u72b6\u6001\u4e0b\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p><strong>1. New\uff08\u65b0\u5efa\uff09\uff1a<\/strong> \u7ebf\u7a0b\u5bf9\u8c61\u88ab\u521b\u5efa\u4f46\u5c1a\u672a\u542f\u52a8\u65f6\u7684\u72b6\u6001\u3002\u6b64\u65f6\u7ebf\u7a0b\u5c1a\u672a\u8fdb\u5165\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n\n\n\n<p><strong>2. Runnable\uff08\u8fd0\u884c\uff09\uff1a<\/strong> \u7ebf\u7a0b\u5bf9\u8c61\u8c03\u7528start()\u65b9\u6cd5\u540e\uff0c\u7ebf\u7a0b\u8fdb\u5165Runnable\u72b6\u6001\u3002\u5728Runnable\u72b6\u6001\u4e0b\u7684\u7ebf\u7a0b\u53ef\u80fd\u6b63\u5728\u6267\u884c\uff0c\u4e5f\u53ef\u80fd\u6b63\u5728\u7b49\u5f85CPU\u65f6\u95f4\u7247\u6765\u6267\u884c\u3002<\/p>\n\n\n\n<p><strong>3. Blocked\uff08\u963b\u585e\uff09\uff1a<\/strong> \u7ebf\u7a0b\u5728\u7b49\u5f85\u9501\u7684\u91ca\u653e\u65f6\u8fdb\u5165Blocked\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u5f53\u7ebf\u7a0b\u8bd5\u56fe\u8fdb\u5165\u4e00\u4e2a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u6301\u6709\u7684\u540c\u6b65\u4ee3\u7801\u5757\u65f6\uff0c\u5b83\u5c06\u88ab\u963b\u585e\u3002<\/p>\n\n\n\n<p><strong>4. Waiting\uff08\u7b49\u5f85\uff09\uff1a<\/strong> \u7ebf\u7a0b\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7684\u7279\u5b9a\u64cd\u4f5c\u65f6\u8fdb\u5165Waiting\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u7ebf\u7a0b\u8c03\u7528\u4e86Object.wait()\u65b9\u6cd5\uff0c\u6216\u8005\u5728Lock\u5bf9\u8c61\u4e0a\u8c03\u7528\u4e86LockSupport.park()\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>5. Timed Waiting\uff08\u8ba1\u65f6\u7b49\u5f85\uff09\uff1a<\/strong> \u4e0eWaiting\u72b6\u6001\u7c7b\u4f3c\uff0c\u4f46\u662f\u5728\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u4f1a\u81ea\u52a8\u8fd4\u56de\u5230Runnable\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u7ebf\u7a0b\u8c03\u7528\u4e86Thread.sleep()\u65b9\u6cd5\uff0c\u6216\u8005\u5728Lock\u5bf9\u8c61\u4e0a\u8c03\u7528\u4e86LockSupport.parkNanos()\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>6. Terminated\uff08\u7ec8\u6b62\uff09\uff1a<\/strong> \u7ebf\u7a0b\u6267\u884c\u5b8c\u4efb\u52a1\u540e\u6216\u8005\u51fa\u73b0\u5f02\u5e38\u800c\u7ed3\u675f\u65f6\u8fdb\u5165Terminated\u72b6\u6001\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001<strong>\u533a\u522b<\/strong><\/h2>\n\n\n\n<p>\u6709\u65f6\u5019\u5c06Timed Waiting\u72b6\u6001\u5355\u72ec\u5217\u4e3a\u4e00\u79cd\u72b6\u6001\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u5f3a\u8c03\u7ebf\u7a0b\u5728\u6b64\u72b6\u6001\u4e0b\u662f\u5728\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u4f1a\u81ea\u52a8\u8fd4\u56deRunnable\u72b6\u6001\u3002\u5728\u8ba1\u7b97\u7ebf\u7a0b\u603b\u6570\u65f6\uff0cTimed Waiting\u72b6\u6001\u901a\u5e38\u88ab\u89c6\u4e3aRunnable\u72b6\u6001\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a\u5b83\u4ec5\u662fRunnable\u72b6\u6001\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Thread.getState()\u65b9\u6cd5\u6765\u83b7\u53d6\u7ebf\u7a0b\u7684\u72b6\u6001\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u7ebf\u7a0b\u7684\u72b6\u6001\u4f1a\u662f\u4ee5\u4e0a5\u79cd\uff0c\u800c\u5982\u679c\u7ebf\u7a0b\u8c03\u7528\u4e86sleep()\u6216\u5176\u4ed6\u8ba1\u65f6\u7b49\u5f85\u7684\u65b9\u6cd5\uff0c\u5219\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u8fdb\u5165Timed Waiting\u72b6\u6001\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5ef6\u4f38\u9605\u8bfb<\/strong><\/h2>\n\n\n\n<p><strong>Java\u7ebf\u7a0b\u72b6\u6001\u7684\u7ba1\u7406<\/strong><\/p>\n\n\n\n<p>\u5728Java\u4e2d\uff0c\u7ebf\u7a0b\u72b6\u6001\u7684\u7ba1\u7406\u5bf9\u4e8e\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6b63\u786e\u6027\u81f3\u5173\u91cd\u8981\u3002\u6b63\u786e\u5730\u7ba1\u7406\u7ebf\u7a0b\u72b6\u6001\u53ef\u4ee5\u907f\u514d\u6b7b\u9501\u3001\u6d3b\u9501\u3001\u7ebf\u7a0b\u9965\u997f\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<ol>\n<li><strong>\u907f\u514d\u963b\u585e\u64cd\u4f5c\uff1a<\/strong> \u5c3d\u91cf\u907f\u514d\u5728\u7ebf\u7a0b\u4e2d\u8fdb\u884c\u963b\u585e\u64cd\u4f5c\uff0c\u56e0\u4e3a\u963b\u585e\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u8fdb\u5165Blocked\u6216Timed Waiting\u72b6\u6001\uff0c\u5f71\u54cd\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/li>\n\n\n\n<li><strong>\u5408\u7406\u4f7f\u7528\u540c\u6b65\uff1a<\/strong> \u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u5408\u7406\u5730\u4f7f\u7528\u540c\u6b65\u673a\u5236\uff0c\u5982synchronized\u5173\u952e\u5b57\u6216Lock\u5bf9\u8c61\uff0c\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u95f4\u7684\u7ade\u4e89\u548c\u51b2\u7a81\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u610f\u7ebf\u7a0b\u72b6\u6001\u8f6c\u6362\uff1a<\/strong> \u4e86\u89e3\u7ebf\u7a0b\u72b6\u6001\u7684\u8f6c\u6362\u8fc7\u7a0b\uff0c\u7279\u522b\u662f\u7ebf\u7a0b\u5728Waiting\u548cTimed Waiting\u72b6\u6001\u4e0b\u7684\u884c\u4e3a\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u8bbe\u8ba1\u548c\u8c03\u8bd5\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff1a<\/strong> \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6765\u7ba1\u7406\u7ebf\u7a0b\u662f\u4e00\u79cd\u8f83\u4e3a\u9ad8\u6548\u7684\u65b9\u5f0f\u3002\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u590d\u7528\u7ebf\u7a0b\uff0c\u907f\u514d\u9891\u7e41\u5730\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u8d44\u6e90\u5229\u7528\u7387\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u5e76\u53d1\u5de5\u5177\uff1a<\/strong> Java\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e76\u53d1\u5de5\u5177\uff0c\u5982CountDownLatch\u3001CyclicBarrier\u3001Semaphore\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ba1\u7406\u7ebf\u7a0b\u7684\u72b6\u6001\u548c\u534f\u8c03\u7ebf\u7a0b\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u901a\u5e38\u60c5\u51b5\u4e0bJava\u7ebf\u7a0b\u67095\u79cd\u72b6\u6001\uff0c\u4f46\u6709\u65f6\u4e5f\u53ef\u4ee5\u5c06\u5176\u62c6\u5206\u4e3a6\u79cd\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5982\u4f55\u770b\u5f85\u7ebf\u7a0b\u5728TIMED_WAITING [&hellip;]","protected":false},"author":6,"featured_media":46397,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/46396"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=46396"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/46396\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/46397"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=46396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=46396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=46396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}