{"id":252,"date":"2023-02-02T22:30:47","date_gmt":"2023-02-02T14:30:47","guid":{"rendered":"http:\/\/codermr.com\/?p=252"},"modified":"2023-03-25T23:24:38","modified_gmt":"2023-03-25T15:24:38","slug":"java_mianjing1","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/","title":{"rendered":"Java\u57fa\u7840\/\u9762\u8bd5\u9898"},"content":{"rendered":"\n<p><em>\u672c\u6587\u662f\u7531\u6211\u7684\u7b14\u8bb0\u6574\u7406\u800c\u6210\uff0c\u603b\u7ed3\u4e86\u9762\u8bd5\u4e2d\u5e38\u89c1\u7684\u77e5\u8bc6\u70b9\uff0c\u540c\u65f6\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u4e5f\u662f\u5f88\u591a Java \u5f00\u53d1\u8005\u5bb9\u6613\u641e\u6df7\u5f04\u9519\u7684\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u4e00\u90e8\u5206\uff1a\u9762\u5411\u5bf9\u8c61\u57fa\u7840\u77e5\u8bc6<\/strong><\/h3>\n\n\n\n<p><strong>final \u5728 java \u4e2d\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>final\u4fee\u9970\u7c7b\uff0c<\/strong>\u610f\u5473\u7740\u8be5\u7c7b\u4e0d\u80fd\u518d\u6d3e\u751f\u51fa\u65b0\u7684\u5b50\u7c7b\uff0c\u4e0d\u80fd\u4f5c\u4e3a\u7236\u7c7b\u800c\u88ab\u5b50\u7c7b\u7ee7\u627f\u3002\u56e0\u6b64\u4e00\u4e2a\u7c7b\u4e0d\u80fd\u65e2\u88ab abstract \u58f0\u660e\uff0c\u53c8\u88ab final \u58f0\u660e\u3002<\/p>\n\n\n\n<p><strong>final\u4fee\u9970\u65b9\u6cd5\uff0c<\/strong>\u5f53\u4f7f\u7528 final \u4fee\u9970\u65b9\u6cd5\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u65e0\u6cd5\u88ab\u5b50\u7c7b\u91cd\u5199\u3002\u4f46\u662f\uff0c\u8be5\u65b9\u6cd5\u4ecd\u7136\u53ef\u4ee5\u88ab\u7ee7\u627f\u3002<\/p>\n\n\n\n<p><strong>final\u4fee\u9970\u53d8\u91cf\uff0c<\/strong>\u5fc5\u987b\u5728\u58f0\u660e\u65f6\u7ed9\u51fa\u53d8\u91cf\u7684\u521d\u59cb\u503c\uff0c\u800c\u5728\u4ee5\u540e\u7684\u5f15\u7528\u4e2d\u53ea\u80fd\u8bfb\u53d6\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>String \u5c5e\u4e8e\u57fa\u7840\u7684\u6570\u636e\u7c7b\u578b\u5417\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e0d\u662f\u3002<\/p>\n\n\n\n<p>Java \u4e2d\u7684<strong>\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/strong>\u53ea\u6709 8 \u4e2a\uff1abyte\u3001short\u3001int\u3001long\u3001float\u3001double\u3001char\u3001boolean\uff1b\u9664\u4e86\u57fa\u672c\u7c7b\u578b\uff08primitive type\uff09\uff0c\u5269\u4e0b\u7684\u90fd\u662f\u5f15\u7528\u7c7b\u578b\uff08reference type\uff09\u3002<\/p>\n\n\n\n<p>String \u7c7b\u4f7f\u7528  final \u4fee\u9970\uff0c\u65e0\u6cd5\u88ab\u7ee7\u627f\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>String \u548c StringBuilder\u3001StringBuffer \u7684\u533a\u522b\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>String<\/strong>\uff1aString \u7684\u503c\u88ab\u521b\u5efa\u540e\u4e0d\u80fd\u4fee\u6539\uff0c\u4efb\u4f55\u5bf9 String \u7684\u4fee\u6539\u90fd\u4f1a\u5f15\u53d1\u65b0\u7684 String \u5bf9\u8c61\u7684\u751f\u6210\u3002<\/p>\n\n\n\n<p><strong>StringBuffer<\/strong>\uff1a\u8ddf String \u7c7b\u4f3c\uff0c\u4f46\u662f\u503c\u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u4f7f\u7528 synchronized \u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n\n\n\n<p><strong>StringBuilder<\/strong>\uff1aStringBuffer \u7684\u975e\u7ebf\u7a0b\u5b89\u5168\u7248\u672c\uff0c\u6ca1\u6709\u4f7f\u7528 synchronized\uff0c\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\uff0c\u63a8\u8350\u4f18\u5148\u4f7f\u7528\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>String s = new String(&#8220;xyz&#8221;) \u521b\u5efa\u4e86\u51e0\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e00\u4e2a\u6216\u4e24\u4e2a\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u5df2\u7ecf\u6709\u201cxyz\u201d\uff0c\u5219\u662f\u4e00\u4e2a\uff1b\u5426\u5219\uff0c\u4e24\u4e2a\u3002<br>\u5f53\u5b57\u7b26\u521b\u5e38\u91cf\u6c60\u6ca1\u6709 \u201cxyz\u201d\uff0c\u6b64\u65f6\u4f1a\u521b\u5efa\u5982\u4e0b\u4e24\u4e2a\u5bf9\u8c61\uff1a<br>\u4e00\u4e2a\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf &#8220;xyz&#8221; \u6240\u5bf9\u5e94\u7684\u3001\u9a7b\u7559\uff08intern\uff09\u5728\u4e00\u4e2a\u5168\u5c40\u5171\u4eab\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u7684\u5b9e\u4f8b\uff0c\u6b64\u65f6\u8be5\u5b9e\u4f8b\u4e5f\u662f\u5728\u5806\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u53ea\u653e\u5f15\u7528\u3002<br>\u53e6\u4e00\u4e2a\u662f\u901a\u8fc7 new String() \u521b\u5efa\u5e76\u521d\u59cb\u5316\u7684\uff0c\u5185\u5bb9\u4e0e&#8221;xyz&#8221;\u76f8\u540c\u7684\u5b9e\u4f8b\uff0c\u4e5f\u662f\u5728\u5806\u4e2d\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>String s = &#8220;xyz&#8221; \u548c String s = new String(&#8220;xyz&#8221;) \u533a\u522b\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e24\u4e2a\u8bed\u53e5\u90fd\u4f1a\u5148\u53bb\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b58\u5728 \u201cxyz\u201d\uff0c\u5982\u679c\u6709\u5219\u76f4\u63a5\u4f7f\u7528\uff0c\u5982\u679c\u6ca1\u6709\u5219\u4f1a\u5728\u5e38\u91cf\u6c60\u4e2d\u521b\u5efa \u201cxyz\u201d \u5bf9\u8c61\u3002<br>\u53e6\u5916\uff0cString s = new String(&#8220;xyz&#8221;) \u8fd8\u4f1a\u901a\u8fc7 new String() \u5728\u5806\u91cc\u521b\u5efa\u4e00\u4e2a\u5185\u5bb9\u4e0e &#8220;xyz&#8221; \u76f8\u540c\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002<br>\u6240\u4ee5\u524d\u8005\u5176\u5b9e\u7406\u89e3\u4e3a\u88ab\u540e\u8005\u7684\u6240\u5305\u542b\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>== \u548c equals \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>== \u8fd0\u7b97\u7b26<\/strong>\uff0c\u7528\u4e8e\u6bd4\u8f83\u57fa\u7840\u7c7b\u578b\u53d8\u91cf\u548c\u5f15\u7528\u7c7b\u578b\u53d8\u91cf\u3002<br>\u5bf9\u4e8e\u57fa\u7840\u7c7b\u578b\u53d8\u91cf\uff0c\u6bd4\u8f83\u7684\u53d8\u91cf\u4fdd\u5b58\u7684\u503c\u662f\u5426\u76f8\u540c\uff0c\u7c7b\u578b\u4e0d\u4e00\u5b9a\u8981\u76f8\u540c\u3002<br>\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u53d8\u91cf\uff0c\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u662f\u5426\u76f8\u540c\u3002<\/p>\n\n\n\n<p><strong>equals <\/strong>\u5728 Object \u65b9\u6cd5\u4e2d\u5176\u5b9e\u7b49\u540c\u4e8e ==\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u7684\u4f7f\u7528\u4e2d\uff0cequals \u901a\u5e38\u88ab\u91cd\u5199\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u540c\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u4e24\u4e2a\u5bf9\u8c61\u7684 hashCode() \u76f8\u540c\uff0c\u5219 equals() \u4e5f\u4e00\u5b9a\u4e3a true\uff0c\u5bf9\u5417\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e0d\u5bf9\u3002<\/p>\n\n\n\n<p>hashCode() \u548c equals() \u4e4b\u95f4\u7684\u5173\u7cfb\u5982\u4e0b\uff1a<br>\u5f53\u6709 a.equals(b) == true \u65f6\uff0c\u5219 a.hashCode() == b.hashCode() \u5fc5\u7136\u6210\u7acb\uff0c<br>\u53cd\u8fc7\u6765\uff0c\u5f53 a.hashCode() == b.hashCode() \u65f6\uff0ca.equals(b) \u4e0d\u4e00\u5b9a\u4e3a true\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u91cd\u5199 equals \u65b9\u6cd5\u9700\u8981\u91cd\u5199 hashCode \u65b9\u6cd5\u5417\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u662f\u7684\u3002<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">JDK\u89c4\u5b9a\uff1a\u5982\u679c\u6839\u636e equals(Object) \u65b9\u6cd5\uff0c\u4e24\u4e2a\u5bf9\u8c61\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u5bf9\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5bf9\u8c61\u8c03\u7528 hashCode \u65b9\u6cd5\u90fd\u5fc5\u987b\u751f\u6210\u76f8\u540c\u7684\u6574\u6570\u7ed3\u679c\u3002<\/mark>\u5982\u679c\u6839\u636e equals(java.lang.Object) \u65b9\u6cd5\uff0c\u4e24\u4e2a\u5bf9\u8c61\u4e0d\u76f8\u7b49\uff0c\u90a3\u4e48\u5bf9\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u4e2d\u7684\u4efb\u4e00\u5bf9\u8c61\u4e0a\u8c03\u7528 hashCode \u65b9\u6cd5\u4e0d \u8981\u6c42\u4e00\u5b9a\u751f\u6210\u4e0d\u540c\u7684\u6574\u6570\u7ed3\u679c\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u91cd\u5199 hashCode \u65b9\u6cd5\u7528\u91cd\u5199 equals \u65b9\u6cd5\u5417\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e0d\u7528\u3002<\/p>\n\n\n\n<p>\u60f3\u5f04\u660e\u767d\u8fd9\u4e2a\u95ee\u9898\u9996\u5148\u9700\u8981\u77e5\u9053 equals\u65b9\u6cd5\u548c equals\u65b9\u6cd5\u7684\u542b\u4e49\u3002<br>equals\u65b9\u6cd5\u7684\u542b\u4e49\uff0c\u5373\u201c\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49\u201d\uff0chashCode \u65b9\u6cd5\u4e00\u822c\u7528\u4e8e\u5728 Map \u3001HashSet \u96c6\u5408\u7c7b\u578b\u4e2d\u8ba1\u7b97\u5bf9\u8c61\u6240\u5904\u7684\u4e0b\u6807\u65f6\u4f7f\u7528\uff0c\u90a3\u4e48\u7406\u6240\u5f53\u7136\u201c\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49\u5219\u5b83\u4eec\u5e94\u8be5\u4f4d\u4e8e\u76f8\u540c\u7684\u4e0b\u6807\u201d\uff0c\u6240\u4ee5\u91cd\u5199  equals \u5e94\u8be5\u91cd\u5199 hashcode \u3002\u53cd\u4e4b\uff0c\u91cd\u5199  hashCode \u65b9\u6cd5\u5e76\u6ca1\u6709\u6d89\u53ca\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u91cd\u5199hashCode \u4e0d\u7528\u91cd\u5199 equals.<\/p>\n<\/blockquote>\n\n\n\n<p><strong>throw \u548c throws \u7684\u533a\u522b\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>throws \u7528\u5728\u65b9\u6cd5\u58f0\u660e\u540e\u9762\uff0c\u8868\u793a\u8be5\u65b9\u6cd5\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\uff0c\u7531\u8be5\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u6765\u5904\u7406\u3002<\/p>\n\n\n\n<p>throw \u662f\u5728\u4ee3\u7801\u4e2d\u5411\u5916\u629b\u5f02\u5e38\u7684\uff0c\u629b\u51fa\u7684\u662f\u4e00\u4e2a\u5f02\u5e38\u5b9e\u4f8b\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>Java \u662f\u503c\u4f20\u9012\u5417\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u503c\u4f20\u9012\u3002<\/p>\n\n\n\n<p>Java \u4e2d\u53ea\u6709\u503c\u4f20\u9012\uff0c\u5bf9\u4e8e\u5bf9\u8c61\u53c2\u6570\uff0c\u503c\u7684\u5185\u5bb9\u662f\u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u91cd\u8f7d\uff08Overload\uff09\u548c\u91cd\u5199\uff08Override\uff09\u7684\u533a\u522b\uff1f<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u91cd\u8f7d\uff1a<\/strong>\u4e00\u4e2a\u7c7b\u4e2d\u6709\u591a\u4e2a\u540c\u540d\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u5177\u6709\u6709\u4e0d\u540c\u7684\u53c2\u6570\u5217\u8868\uff08\u53c2\u6570\u7c7b\u578b\u4e0d\u540c\u3001\u53c2\u6570\u4e2a\u6570\u4e0d\u540c\u6216\u8005\u4e8c\u8005\u90fd\u4e0d\u540c\uff09\u3002<\/p>\n\n\n\n<p><br><strong>\u91cd\u5199\uff1a<\/strong>\u53d1\u751f\u5728\u5b50\u7c7b\u4e0e\u7236\u7c7b\u4e4b\u95f4\uff0c\u5b50\u7c7b\u5bf9\u7236\u7c7b\u7684\u65b9\u6cd5\u8fdb\u884c\u91cd\u5199\uff0c\u53c2\u6570\u90fd\u4e0d\u80fd\u6539\u53d8\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u53ef\u4ee5\u4e0d\u76f8\u540c\uff0c\u4f46\u662f\u5fc5\u987b\u662f\u7236\u7c7b\u8fd4\u56de\u503c\u7684\u6d3e\u751f\u7c7b\u3002\u5373\u5916\u58f3\u4e0d\u53d8\uff0c\u6838\u5fc3\u91cd\u5199\uff01\u91cd\u5199\u7684\u597d\u5904\u5728\u4e8e\u5b50\u7c7b\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u5b9a\u4e49\u7279\u5b9a\u4e8e\u81ea\u5df1\u7684\u884c\u4e3a\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"font-style:normal;font-weight:700\"><strong>\u7b2c\u4e8c\u90e8\u5206\uff1a\u7c7b\u52a0\u8f7d\u76f8\u5173<\/strong><\/h3>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>1. <strong>\u4ec0<\/strong>\u4e48\u662f\u7c7b\u52a0\u8f7d\uff1f<\/strong><\/h5>\n\n\n\n<p>\u7c7b\u52a0\u8f7d\u5c31\u662f\u628a\u7f16\u8bd1\u540e\u7684 .class \u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\uff0c\u7136\u540e JVM \u624d\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u6821\u9a8c\u3001\u89e3\u6790\u3001\u521d\u59cb\u5316\u7b49\u4e00\u4e9b\u5217\u64cd\u4f5c\u6765\u751f\u6210 Java \u53ef\u76f4\u63a5\u4f7f\u7528\u7684\u7c7b\u578b\u3002\u6bcf\u4e2a\u7c7b\u88ab\u52a0\u8f7d\u8fdb\u5165\u5185\u5b58\u4e4b\u540e\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u8be5\u7c7b\u751f\u6210\u4e00\u4e2a Class \u5bf9\u8c61\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>2. \u7c7b\u52a0\u8f7d\u5206\u4e3a\u51e0\u4e2a\u9636\u6bb5\uff1f<\/strong><\/h5>\n\n\n\n<p>\u6309\u7167 Java \u865a\u62df\u673a\u89c4\u8303\uff0c\u4ece Class \u6587\u4ef6\u5230\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684\u7c7b\uff0c\u5230\u7c7b\u5378\u8f7d\u51fa\u5185\u5b58\u4f4d\u7f6e\uff0c\u5b83\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u5305\u62ec\u5982\u4e0b\u4e03\u4e2a\u9636\u6bb5\uff1a<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-2.png\" alt=\"\" class=\"wp-image-256\" width=\"482\" height=\"191\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-2.png 643w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-2-300x119.png 300w\" sizes=\"(max-width: 482px) 100vw, 482px\" \/><figcaption class=\"wp-element-caption\">\u56fe\u7247\u6765\u81ea\u300a\u6df1\u5165\u7406\u89e3Java\u865a\u62df\u673a\u300b<\/figcaption><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">3. <strong>\u4ec0\u4e48\u662f\u7c7b\u52a0\u8f7d\u5668\uff1f<\/strong><\/h5>\n\n\n\n<p><em>\u7c7b\u52a0\u8f7d\u5668\u662f\u4e00\u4e2a\u7528\u6765\u52a0\u8f7d\u7c7b\u6587\u4ef6\u7684\u7c7b\u3002<\/em><\/p>\n\n\n\n<p>Java \u6e90\u4ee3\u7801\u901a\u8fc7 javac \u7f16\u8bd1\u5668\u7f16\u8bd1\u6210\u7c7b\u6587\u4ef6\u3002\u7136\u540e JVM \u6765\u6267\u884c\u7c7b\u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u7801\u6765\u6267\u884c\u7a0b\u5e8f\u3002\u7c7b\u52a0\u8f7d\u5668\u8d1f\u8d23\u52a0\u8f7d\u6587\u4ef6\u7cfb\u7edf\u3001\u7f51\u7edc\u6216\u5176\u4ed6\u6765\u6e90\u7684\u7c7b\u6587\u4ef6\u3002<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\"><strong>Java \u6709\u4e09\u79cd\u9ed8\u8ba4\u4f7f\u7528\u7684\u7c7b\u52a0\u8f7d\u5668\uff1a<\/strong><\/mark><\/p>\n\n\n\n<ul>\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">Bootstrap\u7c7b\u52a0\u8f7d\u5668<\/mark><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">Extension\u7c7b\u52a0\u8f7d\u5668<\/mark><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">System\u7c7b\u52a0\u8f7d\u5668\uff08\u6216\u8005\u53eb\u4f5cApplication\u7c7b\u52a0\u8f7d\u5668\uff09<\/mark><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">Extension\u7c7b\u52a0\u8f7d\u5668<\/mark>\u5c06\u52a0\u8f7d\u7c7b\u7684\u8bf7\u6c42\u5148\u59d4\u6258\u7ed9\u5b83\u7684\u7236\u52a0\u8f7d\u5668\uff0c\u4e5f\u5c31\u662fBootstrap\uff0c\u5982\u679c\u6ca1\u6709\u6210\u529f\u52a0\u8f7d\u7684\u8bdd\uff0c\u518d\u4ece jre\/lib\/ext \u76ee\u5f55\u4e0b\u6216\u8005 java.ext.dirs \u7cfb\u7edf\u5c5e\u6027\u5b9a\u4e49\u7684\u76ee\u5f55\u4e0b\u52a0\u8f7d\u7c7b\u3002Extension\u52a0\u8f7d\u5668\u7531sun.misc.Launcher$ExtClassLoader\u5b9e\u73b0\u3002<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">Bootstrap\u7c7b\u52a0\u8f7d\u5668<\/mark>\u8d1f\u8d23\u52a0\u8f7d <strong>rt.jar<\/strong> \u4e2d\u7684JDK\u7c7b\u6587\u4ef6\uff0c\u5b83\u662f\u6240\u6709\u7c7b\u52a0\u8f7d\u5668\u7684\u7236\u52a0\u8f7d\u5668\u3002Bootstrap\u7c7b\u52a0\u8f7d\u5668\u6ca1\u6709\u4efb\u4f55\u7236\u7c7b\u52a0\u8f7d\u5668\uff0c<strong><em>\u5982\u679c\u4f60\u8c03\u7528String.class.getClassLoader()\uff0c\u4f1a\u8fd4\u56denull<\/em><\/strong>\uff0c\u4efb\u4f55\u57fa\u4e8e\u6b64\u7684\u4ee3\u7801\u4f1a\u629b\u51faNUllPointerException\u5f02\u5e38\u3002Bootstrap\u52a0\u8f7d\u5668\u88ab\u79f0\u4e3a\u521d\u59cb\u7c7b\u52a0\u8f7d\u5668\u3002<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">System \u7c7b\u52a0\u8f7d\u5668<\/mark>\uff08\u53c8\u53eb\u4f5c Application \u7c7b\u52a0\u8f7d\u5668\uff09\u3002<strong>\u5b83\u8d1f\u8d23\u4ececlasspath\u73af\u5883\u53d8\u91cf\u4e2d\u52a0\u8f7d\u67d0\u4e9b\u5e94\u7528\u76f8\u5173\u7684\u7c7b<\/strong>\u3002<\/p>\n\n\n\n<p class=\"has-small-font-size\"><\/p>\n<\/blockquote>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\"><strong>\u603b\u7ed3\u4e00\u4e0b\u4e09\u79cd\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u7c7b\u6587\u4ef6\u7684\u5730\u65b9\uff1a<\/strong><\/mark><\/p>\n\n\n\n<ul class=\"has-small-font-size\">\n<li>Bootstrap\u7c7b\u52a0\u8f7d\u5668 \u2013 JRE\/lib\/rt.jar<\/li>\n\n\n\n<li>Extension\u7c7b\u52a0\u8f7d\u5668 \u2013 JRE\/lib\/ext\u6216\u8005java.ext.dirs\u6307\u5411\u7684\u76ee\u5f55<\/li>\n\n\n\n<li>Application\u7c7b\u52a0\u8f7d\u5668 \u2013 CLASSPATH\u73af\u5883\u53d8\u91cf, \u7531-classpath\u6216-cp\u9009\u9879\u5b9a\u4e49,\u6216\u8005\u662fJAR\u4e2d\u7684Manifest\u7684classpath\u5c5e\u6027\u5b9a\u4e49.<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">4. <strong>\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u7c7b\u52a0\u8f7d\uff1f<\/strong><\/h5>\n\n\n\n<ol class=\"has-small-font-size\">\n<li>\u9047\u5230 new \u5173\u952e\u5b57\u7684\u65f6\u5019<\/li>\n\n\n\n<li>\u4f7f\u7528\u53cd\u5c04\u751f\u6210 Class \u5bf9\u8c61\u7684\u65f6\u5019\u5982 Class.forName(&#8230;)<\/li>\n\n\n\n<li>\u521d\u59cb\u5316\u4e00\u4e2a\u7c7b\u65f6<\/li>\n\n\n\n<li>JVM \u542f\u52a8\u65f6\u6307\u5b9a\u4e86 main \u65b9\u6cd5\u65f6\u3002<\/li>\n<\/ol>\n\n\n\n<h5 class=\"wp-block-heading\">5. <strong>\u7c7b\u52a0\u8f7d\u5668\u7684\u673a\u5236\/\u5de5\u4f5c\u539f\u7406\uff1f<\/strong><\/h5>\n\n\n\n<p>Java\u7c7b\u52a0\u8f7d\u5668\u7684\u5de5\u4f5c\u57fa\u4e8e\u4e09\u4e2a\u673a\u5236\uff1a<mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">\u59d4\u6258\u3001\u53ef\u89c1\u6027\u548c\u5355\u4e00\u6027<\/mark>\u3002<\/p>\n\n\n\n<ul>\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">\u59d4\u6258\u673a\u5236<\/mark>\u662f\u6307\u5c06\u52a0\u8f7d\u4e00\u4e2a\u7c7b\u7684\u8bf7\u6c42\u4ea4\u7ed9\u7236\u7c7b\u52a0\u8f7d\u5668\uff0c\u5982\u679c\u8fd9\u4e2a\u7236\u7c7b\u52a0\u8f7d\u5668\u4e0d\u80fd\u591f\u627e\u5230\u6216\u8005\u52a0\u8f7d\u8fd9\u4e2a\u7c7b\uff0c\u90a3\u4e48\u518d\u52a0\u8f7d\u5b83\u3002<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">\u53ef\u89c1\u6027<\/mark>\u7684\u539f\u7406\u662f\u5b50\u7c7b\u7684\u52a0\u8f7d\u5668\u53ef\u4ee5\u770b\u89c1\u6240\u6709\u7684\u7236\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u7684\u7c7b\uff0c\u800c\u7236\u7c7b\u52a0\u8f7d\u5668\u770b\u4e0d\u5230\u5b50\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u7684\u7c7b\u3002<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">\u5355\u4e00\u6027\u539f\u7406<\/mark>\u662f\u6307\u4ec5\u52a0\u8f7d\u4e00\u4e2a\u7c7b\u4e00\u6b21\uff0c\u8fd9\u662f\u7531\u59d4\u6258\u673a\u5236\u786e\u4fdd\u5b50\u7c7b\u52a0\u8f7d\u5668\u4e0d\u4f1a\u518d\u6b21\u52a0\u8f7d\u7236\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u8fc7\u7684\u7c7b\u3002\u6b63\u786e\u7406\u89e3\u7c7b\u52a0\u8f7d\u5668\u80fd\u591f\u5e2e\u4f60\u89e3\u51b3 NoClassDefFoundError \u548cjava.lang.ClassNotFoundException\uff0c\u56e0\u4e3a\u5b83\u4eec\u548c\u7c7b\u7684\u52a0\u8f7d\u76f8\u5173\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u59d4\u6258\u673a\u5236<\/strong>\u5f53\u4e00\u4e2a\u7c7b\u52a0\u8f7d\u548c\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u7c7b\u4ec5\u5728\u6709\u9700\u8981\u52a0\u8f7d\u7684\u65f6\u5019\u88ab\u52a0\u8f7d\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5e94\u7528\u9700\u8981\u7684\u7c7b\u53eb\u4f5c Abc.class\uff0c\u9996\u5148\u52a0\u8f7d\u8fd9\u4e2a\u7c7b\u7684\u8bf7\u6c42\u7531Application \u7c7b\u52a0\u8f7d\u5668\u59d4\u6258\u7ed9\u5b83\u7684\u7236\u7c7b\u52a0\u8f7d\u5668 Extension \u7c7b\u52a0\u8f7d\u5668\uff0c\u7136\u540e\u518d\u59d4\u6258\u7ed9Bootstrap\u7c7b\u52a0\u8f7d\u5668\u3002Bootstrap \u7c7b\u52a0\u8f7d\u5668\u4f1a\u5148\u770b\u770b rt.jar \u4e2d\u6709\u6ca1\u6709\u8fd9\u4e2a\u7c7b\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u8fd9\u4e2a\u7c7b\uff0c\u6240\u4ee5\u8fd9\u4e2a\u8bf7\u6c42\u7531\u56de\u5230 Extension \u7c7b\u52a0\u8f7d\u5668\uff0c\u5b83\u4f1a\u67e5\u770bjre\/lib\/ext\u76ee\u5f55\u4e0b\u6709\u6ca1\u6709\u8fd9\u4e2a\u7c7b\uff0c\u5982\u679c\u8fd9\u4e2a\u7c7b\u88ab Extension \u7c7b\u52a0\u8f7d\u5668\u627e\u5230\u4e86\uff0c\u90a3\u4e48\u5b83\u5c06\u88ab\u52a0\u8f7d\uff0c\u800c Application \u7c7b\u52a0\u8f7d\u5668\u4e0d\u4f1a\u52a0\u8f7d\u8fd9\u4e2a\u7c7b\uff1b\u800c\u5982\u679c\u8fd9\u4e2a\u7c7b\u6ca1\u6709\u88ab Extension \u7c7b\u52a0\u8f7d\u5668\u627e\u5230\uff0c\u90a3\u4e48\u518d\u7531 Application \u7c7b\u52a0\u8f7d\u5668\u4ececlasspath\u4e2d\u5bfb\u627e\u3002\u8bb0\u4f4f classpath \u5b9a\u4e49\u7684\u662f\u7c7b\u6587\u4ef6\u7684\u52a0\u8f7d\u76ee\u5f55\uff0c\u800c PATH \u662f\u5b9a\u4e49\u7684\u662f\u53ef\u6267\u884c\u7a0b\u5e8f\u5982 javac\uff0cjava \u7b49\u7684\u6267\u884c\u8def\u5f84\u3002<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u53ef\u89c1\u6027\u673a\u5236<\/strong>\u6839\u636e\u53ef\u89c1\u6027\u673a\u5236\uff0c\u5b50\u7c7b\u52a0\u8f7d\u5668\u53ef\u4ee5\u770b\u5230\u7236\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u7684\u7c7b\uff0c\u800c\u53cd\u4e4b\u5219\u4e0d\u884c\u3002<strong>\u6240\u4ee5\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53Abc.class\u5df2\u7ecf\u88abApplication\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u8fc7\u4e86\uff0c\u7136\u540e\u5982\u679c\u60f3\u8981\u4f7f\u7528Extension\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u8fd9\u4e2a\u7c7b\uff0c\u5c06\u4f1a\u629b\u51fajava.lang.ClassNotFoundException\u5f02\u5e38<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5355\u4e00\u6027\u673a\u5236<\/strong><strong>\u6839\u636e\u8fd9\u4e2a\u673a\u5236\uff0c\u7236\u52a0\u8f7d\u5668\u52a0\u8f7d\u8fc7\u7684\u7c7b\u4e0d\u80fd\u88ab\u5b50\u52a0\u8f7d\u5668\u52a0\u8f7d\u7b2c\u4e8c\u6b21<\/strong>\u3002\u867d\u7136\u91cd\u5199\u8fdd\u53cd\u59d4\u6258\u548c\u5355\u4e00\u6027\u673a\u5236\u7684\u7c7b\u52a0\u8f7d\u5668\u662f\u53ef\u80fd\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u5e76\u4e0d\u53ef\u53d6\u3002\u4f60\u5199\u81ea\u5df1\u7684\u7c7b\u52a0\u8f7d\u5668\u7684\u65f6\u5019\u5e94\u8be5\u4e25\u683c\u9075\u5b88\u8fd9\u4e09\u6761\u673a\u5236\u3002<\/p>\n<\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\">6. <strong>\u5982\u4f55\u663e\u5f0f\u7684\u52a0\u8f7d<strong>\u7c7b<\/strong>\uff1f<\/strong><\/h5>\n\n\n\n<p>Java\u63d0\u4f9b\u4e86\u663e\u5f0f\u52a0\u8f7d\u7c7b\u7684API\uff1aClass.forName(classname) \u548c Class.forName(classname, initialized, classloader)\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>7. \u7b80\u8981\u63cf\u8ff0 ClassLoader \u52a0\u8f7d\u7c7b\u7684\u8fc7\u7a0b<\/strong><\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>ClassLoader\u4f7f\u7528\u7684\u662f<strong>\u53cc\u4eb2\u59d4\u6258\u6a21\u578b<\/strong>\u6765\u641c\u7d22\u7c7b\u7684\uff0c\u6bcf\u4e2a  ClassLoader \u5b9e\u4f8b\uff08\u6bd4\u5982\u6269\u5c55\u7c7b\u52a0\u8f7d\u5668\u3001\u7cfb\u7edf\u7c7b\u52a0\u8f7d\u5668\uff09\u90fd\u6709\u4e00\u4e2a\u7236\u7c7b\u52a0\u8f7d\u5668\u7684\u5f15\u7528\uff08<strong>\u4e0d\u662f\u7ee7\u627f\u7684\u5173\u7cfb\uff0c\u662f\u4e00\u4e2a\u5305\u542b\u7684\u5173\u7cfb<\/strong>\uff09\uff0c\u865a\u62df\u673a\u5185\u7f6e\u7684\u7c7b\u52a0\u8f7d\u5668\uff08Bootstrap ClassLoader\uff09\u672c\u8eab\u6ca1\u6709\u7236\u7c7b\u52a0\u8f7d\u5668\uff0c\u4f46\u53ef\u4ee5\u7528\u4f5c\u5176\u5b83 ClassLoader \u5b9e\u4f8b\u7684\u7684\u7236\u7c7b\u52a0\u8f7d\u5668\u3002\u5f53\u4e00\u4e2aClassLoader\u5b9e\u4f8b\u9700\u8981\u52a0\u8f7d\u67d0\u4e2a\u7c7b\u65f6\uff0c\u5b83\u4f1a\u5148\u628a\u8fd9\u4e2a\u4efb\u52a1\u59d4\u6258\u7ed9\u5b83\u7684\u7236\u7c7b\u52a0\u8f7d\u5668\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u7531\u4e0a\u81f3\u4e0b\u4f9d\u6b21\u68c0\u67e5\u7684\uff0c<strong>\u9996\u5148\u7531\u6700\u9876\u5c42\u7684\u7c7b\u52a0\u8f7d\u5668 Bootstrap ClassLoader\u8bd5\u56fe\u52a0\u8f7d\uff0c\u5982\u679c\u6ca1\u52a0\u8f7d\u5230\uff0c\u5219\u628a\u4efb\u52a1\u8f6c\u4ea4\u7ed9Extension ClassLoader\u8bd5\u56fe\u52a0\u8f7d\uff0c\u5982\u679c\u4e5f\u6ca1\u52a0\u8f7d\u5230\uff0c\u5219\u8f6c\u4ea4\u7ed9App ClassLoader \u8fdb\u884c\u52a0\u8f7d\uff0c\u5982\u679c\u5b83\u4e5f\u6ca1\u6709\u52a0\u8f7d\u5f97\u5230\u7684\u8bdd\uff0c\u5219\u8fd4\u56de\u7ed9\u59d4\u6258\u7684\u53d1\u8d77\u8005\uff0c\u7531\u5b83\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u7cfb\u7edf\u6216\u7f51\u7edc\u7b49URL\u4e2d\u52a0\u8f7d\u8be5\u7c7b\u3002\u5982\u679c\u5b83\u4eec\u90fd\u6ca1\u6709\u52a0\u8f7d\u5230\u8fd9\u4e2a\u7c7b\u65f6\uff0c\u5219\u629b\u51faClassNotFoundException\u5f02\u5e38\u3010\u91cd\u8981\uff01\u3011<\/strong><strong>\u3002<\/strong>\u5426\u5219\u5c06\u8fd9\u4e2a\u627e\u5230\u7684\u7c7b\u751f\u6210\u4e00\u4e2a\u7c7b\u7684\u5b9a\u4e49\uff0c\u5e76\u5c06\u5b83\u52a0\u8f7d\u5230\u5185\u5b58\u5f53\u4e2d\uff0c\u6700\u540e\u8fd4\u56de\u8fd9\u4e2a\u7c7b\u5728\u5185\u5b58\u4e2d\u7684 Class \u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n<\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\">8. <strong>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u53cc\u4eb2\u59d4\u6258\u8fd9\u79cd\u6a21\u578b\u5462\uff1f<\/strong><\/h5>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u52a0\u8f7d\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">9. <strong>JVM \u5728\u641c\u7d22\u7c7b\u7684\u65f6\u5019\uff0c\u53c8\u662f\u5982\u4f55\u5224\u5b9a\u4e24\u4e2aclass\u662f\u76f8\u540c\u7684\u5462\uff1f<\/strong><\/h5>\n\n\n\n<p>JVM \u5728\u5224\u5b9a\u4e24\u4e2a class \u662f\u5426\u76f8\u540c\u65f6\uff0c\u4e0d\u4ec5\u8981\u5224\u65ad\u4e24\u4e2a\u7c7b\u540d\u662f\u5426\u76f8\u540c\uff0c\u800c\u4e14\u8981\u5224\u65ad\u662f\u5426\u7531\u540c\u4e00\u4e2a\u7c7b\u52a0\u8f7d\u5668\u5b9e\u4f8b\u52a0\u8f7d\u7684\u3002\u53ea\u6709\u4e24\u8005\u540c\u65f6\u6ee1\u8db3\u7684\u60c5\u51b5\u4e0b\uff0cJVM \u624d\u8ba4\u4e3a\u8fd9\u4e24\u4e2a class \u662f\u76f8\u540c\u7684\u3002\u5c31\u7b97\u4e24\u4e2aclass\u662f\u540c\u4e00\u4efd class \u5b57\u8282\u7801\uff0c\u5982\u679c\u88ab\u4e24\u4e2a\u4e0d\u540c\u7684 ClassLoader \u5b9e\u4f8b\u6240\u52a0\u8f7d\uff0cJVM \u4e5f\u4f1a\u8ba4\u4e3a\u5b83\u4eec\u662f\u4e24\u4e2a\u4e0d\u540cclass\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\u7f51\u7edc\u4e0a\u7684\u4e00\u4e2a Java \u7c7b <a href=\"http:\/\/org.classloader.simple.netclassloadersimple\/\">org.classloader.simple.NetClassLoaderSimple<\/a>\uff0cjavac  \u7f16\u8bd1\u4e4b\u540e\u751f\u6210\u5b57\u8282\u7801\u6587\u4ef6NetClassLoaderSimple.class\uff0cClassLoaderA \u548c ClassLoaderB \u8fd9\u4e24\u4e2a\u7c7b\u52a0\u8f7d\u5668\u5e76\u8bfb\u53d6\u4e86NetClassLoaderSimple.class \u6587\u4ef6\uff0c\u5e76\u5206\u522b\u5b9a\u4e49\u51fa\u4e86 java.lang.Class \u5b9e\u4f8b\u6765\u8868\u793a\u8fd9\u4e2a\u7c7b\uff0c\u5bf9\u4e8eJVM\u6765\u8bf4\uff0c\u5b83\u4eec\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u4f46\u5b83\u4eec\u786e\u5b9e\u662f\u540c\u4e00\u4efd\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u5982\u679c\u8bd5\u56fe\u5c06\u8fd9\u4e2aClass\u5b9e\u4f8b\u751f\u6210\u5177\u4f53\u7684\u5bf9\u8c61\u8fdb\u884c\u8f6c\u6362\u65f6\uff0c\u5c31\u4f1a\u629b\u8fd0\u884c\u65f6\u5f02\u5e38java.lang.ClassCaseException\uff0c\u63d0\u793a\u8fd9\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u4e09\u90e8\u5206\uff1a\u96c6\u5408\u76f8\u5173<\/strong><\/h3>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0000ff\" class=\"has-inline-color\">1. HashMap \u77e5\u8bc6\u70b9<\/mark><\/strong><\/h5>\n\n\n\n<p><strong>1.1 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">HashMap \u5b9e\u73b0\u539f\u7406\uff1f\u52a0\u8f7d\u56e0\u5b50\uff1f\u6269\u5bb9\u8fc7\u7a0b\uff1f<\/mark><\/strong><\/p>\n\n\n\n<p>HashMap \u5e95\u5c42\u662f\u4f7f\u7528<mark style=\"background-color:#ffff00\" class=\"has-inline-color\">\u201c\u6570\u7ec4+\u94fe\u8868\u201d<\/mark>\u7684\u5f62\u5f0f\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p>\u9ed8\u8ba4\u5bb9\u91cf16\uff0c\u9ed8\u8ba4\u52a0\u8f7d\u56e0\u5b50 0.75\u3002threshold(\u9608\u503c) = \u5bb9\u91cf * \u52a0\u8f7d\u5f15\u5b50\u3002\u5f53\u5143\u7d20\u5bb9\u91cf\u8d85\u8fc7 threshold  \u4fbf\u4f1a\u89e6\u53d1\u6269\u5bb9\u3002HashMap\u6269\u5bb9\u4e00\u6b21\u6269\u5bb91\u500d\u3002HashMap \u9ed8\u8ba4\u6700\u5927\u5bb9\u91cf<strong><em>MAXIMUM_CAPACITY<\/em><\/strong>= 1 &lt;&lt; 30; \u8fd9\u4e2a\u503c\u662f10\u4ebf\u591a\u70b9\uff0c\u5c0f\u4e8eInteger\u6700\u5927\u503c20\u591a\u4ebf\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u6307\u5b9aHashMap\u5bb9\u91cf\u5927\u4e8e<strong><em>MAXIMUM_CAPACITY<\/em><\/strong>\u3002<\/p>\n\n\n\n<p>\u5728HashMap\u7684\u6784\u9020\u5668\u4e2d\uff0c\u4f60\u6307\u5b9a\u4e86\u4e00\u4e2a\u5bb9\u91cf\u503c\uff0c\u7136\u540e\u5185\u90e8\u6570\u7ec4\u7684\u5927\u5c0f\u5e76\u4e0d\u4e00\u5b9a\u662f\u8fd9\u4e2a\u5bb9\u91cf\uff0c\u56e0\u4e3aHashMap\u5185\u90e8\u6570\u7ec4\u7684length\u5fc5\u987b\u662f2\u7684\u6b21\u65b9\u3002\u6240\u4ee5HashMap\u7684\u6784\u9020\u5668\u4f1a\u505a\u5904\u7406\uff0c\u5373\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>int capacity = 1; <br>while(capacity &lt; initialCapacity)<\/p>\n<\/blockquote>\n\n\n\n<p>capacity &lt;&lt;= 1; &nbsp;\u3010\u5373\u5982\u679c\u4f60\u6307\u5b9a\u4e86\u7684\u5bb9\u91cf initialCapacity \u7b49\u4e8e8\uff0c\u5219 capacity \u7b49\u4e8e8\uff1b\u5982\u679cinitialCapacity\u7b49\u4e8e9\uff0c\u5219capacity\u7b49\u4e8e16\u3011==\u300b\u6240\u4ee5\u8fd9\u91cc\u7684capacity\u662f\u6307HashMap\u7684\u771f\u5b9e\u5bb9\u91cf\u3002<\/p>\n\n\n\n<ul class=\"has-small-font-size\">\n<li>\u4e00\u822c\u6765\u8bf4\uff0c<strong>threshold<\/strong> \u7b49\u4e8e capacity \u4e58\u4ee5\u52a0\u8f7d\u56e0\u5b50\u3002<\/li>\n\n\n\n<li>HashMap\u6269\u5bb9\u4e00\u6b21\u6269\u5bb91\u500d\u3002<strong>resize<\/strong>(2 *<strong>table<\/strong>.<strong>length<\/strong>);<\/li>\n\n\n\n<li>HashMap\u6269\u5bb9\u4f1a\u4f7f\u7528\u65b0\u5bb9\u91cf\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u628a\u539f\u6570\u7ec4\u5185\u5bb9 for \u5faa\u73af copy \u5230\u65b0\u6570\u7ec4\u3002\u6269\u5bb9\u7684\u65f6\u5019\u9700\u8981\u91cd\u65b0\u8ba1\u7b97\u54c8\u5e0c\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>1.2 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">HashMap \u5728\u591a\u7ebf\u7a0b\u64cd\u4f5c\u7684\u65f6\u5019\u4f1a\u4ea7\u751f\u54ea\u4e9b\u95ee\u9898\uff1f<\/mark><\/strong><\/p>\n\n\n\n<ul class=\"has-small-font-size\">\n<li>\u4f1a\u4ea7\u751f\u810f\u6570\u636e\u7684\u60c5\u51b5\u3002<\/li>\n\n\n\n<li>\u591a\u7ebf\u7a0b\u64cd\u4f5c\u53ef\u80fd\u4f1a\u51fa\u73b0\u67d0\u4e2a\u7ebf\u7a0b\u88abhang\u4f4f\u7684\u60c5\u51b5\u3002<\/li>\n\n\n\n<li>\u5bf9HashMap\u4e2d\u7684\u5143\u7d20\u8fdb\u884crehash\u8fc7\u7a0b\uff0c\u53ef\u80fd\u4f1a\u8fdb\u884c\u4e24\u6b21\u3002<\/li>\n\n\n\n<li>resize\u6269\u5bb9\uff0c\u53ef\u80fd\u4f1a\u8fdb\u884c\u4e24\u6b21\uff0c\u5bfc\u81f4\u5176\u4e2d\u4e00\u6b21\u7684\u64cd\u4f5c\u88ab\u8986\u76d6\u6389\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>1.3 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">HashMap \u600e\u4e48\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff1f<\/mark><\/strong><\/p>\n\n\n\n<p>\u5f80 HashMap \u4e2d put \u4e00\u4e2a K\/V \u65f6\uff0c\u5982\u679c key \u51fa\u73b0\u78b0\u649e\uff08\u78b0\u649e\u7684\u610f\u601d\u662f\u8ba1\u7b97\u5f97\u5230\u7684 Hash \u503c\u76f8\u540c\uff0c\u9700\u8981\u653e\u5230\u540c\u4e00\u4e2a bucket \u4e2d\uff09\uff0c\u4ee5\u94fe\u8868\u7684\u65b9\u5f0f\u94fe\u63a5\u5230\u540e\u9762\u3002<\/p>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0000ff\" class=\"has-inline-color\">2. CopyOnWriteArrayList<\/mark><\/strong><\/h5>\n\n\n\n<p> CopyOnWriteArrayList \u987e\u540d\u601d\u4e49\uff0c\u201c\u5199\u65f6\u590d\u5236\u201d\u3002<br>\u4efb\u4f55\u5bf9 CopyOnWriteArrayList \u7684\u4fee\u6539\u90fd\u4f1a\u5bfc\u81f4\u6574\u4e2a\u6570\u7ec4\u88ab\u590d\u5236\u3002CopyOnWriteArrayList \u603b\u7ed3\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>set\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u52a0\u53ef\u91cd\u5165\u9501\uff0c\u4e5f\u5c31\u662f\u8bf4\u5bf9\u4e8eCopyOnWriteArrayList\u6765\u8bf4\uff0c\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0bset\u5143\u7d20\u8fdb\u884c\u5199\u3002\u5728\u5199\u64cd\u4f5c\u6ca1\u7ed3\u675f\u4e4b\u524d\uff0c\u8bfb\u64cd\u4f5c\u8bfb\u7684\u90fd\u662f\u65e7\u6570\u7ec4\u91cc\u9762\u7684\u6570\u636e\u3002===\u300b\u300b<strong>CopyOnWriteArrayList<\/strong><strong>\u8fdb\u884c\u5199\u7684\u65f6\u5019\u52a0\u9501\uff0c\u8bfb\u7684\u65f6\u5019\u4e0d\u52a0\u9501\uff01\uff01\uff01<\/strong><\/p>\n<\/blockquote>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0000ff\" class=\"has-inline-color\">3. BlockingQueue<\/mark><\/strong><\/h5>\n\n\n\n<p><mark style=\"background-color:#fffc00\" class=\"has-inline-color\"><strong>BlockingQueue \u7684\u5b9e\u73b0\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684<\/strong>\uff0c<\/mark>\u4f46\u662f\u6279\u91cf\u7684\u96c6\u5408\u64cd\u4f5c\u5982 addAll, containsAll , retainAll \u548c removeAll \u56e0\u4e3a\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\u6240\u4ee5\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n\n\n\n<p><strong>3.1<\/strong> <strong>\u963b\u585e\u961f\u5217\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n\n\n\n<p><em>\u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u8fd9\u4e2a\u65f6\u5019\u8bfb\u64cd\u4f5c\u7684\u7ebf\u7a0b\u8fdb\u5165\u5230\u8bfb\u7ebf\u7a0b\u961f\u5217\u6392\u961f\uff0c\u7b49\u5f85\u5199\u7ebf\u7a0b\u5199\u5165\u65b0\u7684\u5143\u7d20\uff0c\u7136\u540e\u5524\u9192\u8bfb\u7ebf\u7a0b\u961f\u5217\u7684\u7b2c\u4e00\u4e2a\u7b49\u5f85\u7ebf\u7a0b\u3002\u5982\u679c\u961f\u5217\u5df2\u6ee1\uff0c\u8fd9\u4e2a\u65f6\u5019\u5199\u64cd\u4f5c\u7684\u7ebf\u7a0b\u8fdb\u5165\u5230\u5199\u7ebf\u7a0b\u961f\u5217\u6392\u961f\uff0c\u7b49\u5f85\u8bfb\u7ebf\u7a0b\u5c06\u961f\u5217\u5143\u7d20\u79fb\u9664\u817e\u51fa\u7a7a\u95f4\uff0c\u7136\u540e\u5524\u9192\u5199\u7ebf\u7a0b\u961f\u5217\u7684\u7b2c\u4e00\u4e2a\u7b49\u5f85\u7ebf\u7a0b\u3002==&gt;\u6b63\u56e0\u4e3a\u963b\u585e\u961f\u5217\u6709\u8fd9\u4e2a\u201c\u7761\u7720\u201d\u548c\u201c\u5524\u9192\u201d\u7684\u52a8\u4f5c\uff0c\u6240\u4ee5\u963b\u585e\u961f\u5217\u7684\u5b9e\u73b0\u7c7b\uff08\u6bd4\u5982ArrayBlockingQueue\u3001LinkedBlockingQueue\uff09\u4e2d\uff0c\u90fd\u5f97\u6709Condition\u3002<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"184\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-3-1024x184.png?v=1675319717\" alt=\"\" class=\"wp-image-275\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-3-1024x184.png 1024w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-3-300x54.png 300w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-3-768x138.png 768w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-3-1536x276.png 1536w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-3-816x146.png 816w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-3.png 2029w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0000ff\" class=\"has-inline-color\">4. ArrayBlockingQueue<\/mark><\/strong><\/h5>\n\n\n\n<p><em>ArrayBlockingQueue \u4f7f\u7528<strong><em>\u6570\u7ec4\u5b9e\u73b0<\/em><\/strong>\u7684\u963b\u585e\u961f\u5217\uff0c\u5b83\u5b9e\u73b0\u5e76\u53d1\u540c\u6b65\u7684\u539f\u7406\u662f\uff0c\u8bfb\u64cd\u4f5c\u548c\u5199\u64cd\u4f5c\u90fd\u9700\u8981\u83b7\u53d6\u5230 AQS \u72ec\u5360\u9501\uff08\u5373ReentrantLock\uff09\u624d\u80fd\u8fdb\u884c\u64cd\u4f5c\u3002<mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">ArrayBlockingQueue \u8bfb\u548c\u5199\uff0c\u7528\u7684\u662f\u4e00\u628a\u9501\u3002<\/mark><\/em><\/p>\n\n\n\n<p>ArrayBlokckingQueue\u662f<strong><em>\u6570\u7ec4\u5b9e\u73b0<\/em><\/strong>\u7684\u963b\u585e\u961f\u5217\uff0c\u4f46\u8fd9\u4e2a\u963b\u585e\u961f\u5217<strong><em>\u5e76\u4e0d\u662f\u6240\u6709\u65b9\u6cd5\u90fd\u662f\u963b\u585e\u7684<\/em><\/strong>\uff0c\u6bd4\u5982\u5f80\u961f\u5217\u91cc\u9762\u6dfb\u52a0\u5143\u7d20\uff0c\u53ea\u6709put\u65b9\u6cd5\u4f1a\u963b\u585e\uff0c\u963b\u585e\u65f6\u4f7f\u7528\u7684\u662f\u53ef\u91cd\u5165\u9501\u7684await\u65b9\u6cd5\uff0c\u800coffer\u3001add\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u963b\u585e\u3002===\u300b\u300b\u4f46\u4e0d\u8bba\u662f\u5426\u963b\u585e\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u52a0\u4e86\u53ef\u91cd\u5165\u9501\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\uff08\u5373\u963b\u585e\u961f\u5217\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff09\u3002ArrayBlokckingQueue\u662f\u4e00\u4e2a\u5faa\u73af\u6570\u7ec4\uff0c\u5f53put\u7684\u65f6\u5019\u961f\u5217\u6ee1\u4e86\u4f1a\u8bbe\u7f6eputIndex=0\u91cd\u65b0\u4ece\u4e0b\u8868\u7b49\u4e8e0\u7684\u4f4d\u7f6eput\u3002ArrayBlockingQueue\u8bfb\u548c\u5199\uff0c\u7528\u7684\u662f\u4e00\u628a\u9501\u3002\u800cLinkedBlockingQueue\u8bfb\u548c\u5199\uff0c\u7528\u7684\u4e24\u628a\u9501\u3002ArrayBlokckingQueue\u4e3a\u4ec0\u4e48\u8bfb\u5199\u4f7f\u7528\u4e00\u628a\u9501\uff1f\u6211\u89c9\u5f97\u8fd8\u662f\u56e0\u4e3a\u6570\u7ec4\u7684\u5165\u961f\u548c\u51fa\u961f\u65f6\u95f4\u590d\u6742\u5ea6\u4f4e\uff0c\u4e0d\u50cf\u5217\u8868\u9700\u8981\u989d\u5916\u7ef4\u62a4\u8282\u70b9\u5bf9\u8c61\u3002\u6240\u4ee5\u5f53\u5165\u961f\u548c\u51fa\u961f\u5e76\u53d1\u6267\u884c\u65f6\uff0c\u963b\u585e\u65f6\u95f4\u5f88\u77ed\u3002\u5982\u679c\u4f7f\u7528\u53cc\u9501\u7684\u8bdd\uff0c\u4f1a\u5e26\u6765\u989d\u5916\u7684\u8bbe\u8ba1\u590d\u6742\u6027\u3002<\/p>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0000ff\" class=\"has-inline-color\">5. LinkedBlockingQueue<\/mark><\/strong><\/h5>\n\n\n\n<p><em>LinkedBlockingQueue \u662f\u7528\u94fe\u8868\u5b9e\u73b0\u7684\u963b\u585e\u961f\u5217\uff0c\u5b83\u5176\u5b9e\u4e5f\u662f\u6709\u754c\u7684\uff0c\u5927\u5c0f\u662fInteger.MAX_VALUE\uff0821\u4ebf\u591a\uff09\u3002<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-4-1024x346.png?v=1675319802\" alt=\"\" class=\"wp-image-276\" width=\"512\" height=\"173\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-4-1024x346.png 1024w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-4-300x101.png 300w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-4-768x260.png 768w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-4-816x276.png 816w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-4.png 1073w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0000ff\" class=\"has-inline-color\">6. ConcurrentHashMap<\/mark><\/strong><\/h5>\n\n\n\n<p>ConcurrentHashMap \u7684\u5927\u90e8\u5206\u64cd\u4f5c\u548c HashMap \u662f\u76f8\u540c\u7684\uff0c\u4f8b\u5982\u521d\u59cb\u5316\uff0c\u6269\u5bb9\u548c\u94fe\u8868\u5411\u7ea2\u9ed1\u6811\u7684\u8f6c\u53d8\u7b49\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u5728 ConcurrentHashMap \u4e2d\uff0c\u5927\u91cf\u4f7f\u7528\u4e86 U.compareAndSwapXXX \u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5229\u7528\u4e00\u4e2a CAS \u7b97\u6cd5\u5b9e\u73b0\u65e0\u9501\u5316\u7684\u4fee\u6539\u503c\u7684\u64cd\u4f5c\uff0c\u4ed6\u53ef\u4ee5\u5927\u5927\u964d\u4f4e\u9501\u4ee3\u7406\u7684\u6027\u80fd\u6d88\u8017\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u5c31\u662f\u4e0d\u65ad\u5730\u53bb\u6bd4\u8f83\u5f53\u524d\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\u503c\u4e0e\u4f60\u6307\u5b9a\u7684\u4e00\u4e2a\u53d8\u91cf\u503c\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5219\u63a5\u53d7\u4f60\u6307\u5b9a\u7684\u4fee\u6539\u7684\u503c\uff0c\u5426\u5219\u62d2\u7edd\u4f60\u7684\u64cd\u4f5c\u3002\u56e0\u4e3a\u5f53\u524d\u7ebf\u7a0b\u4e2d\u7684\u503c\u5df2\u7ecf\u4e0d\u662f\u6700\u65b0\u7684\u503c\uff0c\u4f60\u7684\u4fee\u6539\u5f88\u53ef\u80fd\u4f1a\u8986\u76d6\u6389\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u7684\u7ed3\u679c\u3002\u8fd9\u4e00\u70b9\u4e0e\u4e50\u89c2\u9501\uff0cSVN\u7684\u601d\u60f3\u662f\u6bd4\u8f83\u7c7b\u4f3c\u7684\u3002<br>\u540c\u65f6\uff0c\u5728ConcurrentHashMap\u4e2d\u8fd8\u5b9a\u4e49\u4e86\u4e09\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u7528\u4e8e\u5bf9\u6307\u5b9a\u4f4d\u7f6e\u7684\u8282\u70b9\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u4e09\u79cd\u539f\u5b50\u64cd\u4f5c\u88ab\u5e7f\u6cdb\u7684\u4f7f\u7528\u5728ConcurrentHashMap\u7684get\u548cput\u7b49\u65b9\u6cd5\u4e2d\uff0c\u6b63\u662f\u8fd9\u4e9b\u539f\u5b50\u64cd\u4f5c\u4fdd\u8bc1\u4e86ConcurrentHashMap\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u73b0\u539f\u7406<\/strong><br>ConcurrentHashMap \u4f7f\u7528<strong><em>\u5206\u6bb5\u9501<\/em><\/strong>\u6280\u672f\uff0c\u5c06\u6570\u636e\u5206\u6210\u4e00\u6bb5\u4e00\u6bb5\u7684\u5b58\u50a8\uff0c\u7136\u540e\u7ed9\u6bcf\u4e00\u6bb5\u6570\u636e\u914d\u4e00\u628a\u9501\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u5360\u7528\u9501\u8bbf\u95ee\u5176\u4e2d\u4e00\u4e2a\u6bb5\u6570\u636e\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u6bb5\u7684\u6570\u636e\u4e5f\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\uff0c\u80fd\u591f\u5b9e\u73b0\u771f\u6b63\u7684\u5e76\u53d1\u8bbf\u95ee\u3002\u5982\u4e0b\u56fe\u662f ConcurrentHashMap \u7684\u5185\u90e8\u7ed3\u6784\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"288\" height=\"304\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-5.png\" alt=\"\" class=\"wp-image-277\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-5.png 288w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-5-284x300.png 284w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/figure>\n\n\n\n<p>\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u5230\uff0cConcurrentHashMap\u5185\u90e8\u5206\u4e3a\u5f88\u591a\u4e2aSegment\uff0c\u6bcf\u4e00\u4e2aSegment\u62e5\u6709\u4e00\u628a\u9501\uff0c\u7136\u540e\u6bcf\u4e2aSegment\uff08\u7ee7\u627fReentrantLock\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static final class Segment &lt;K,V&gt; extends ReentrantLock implements Serializable<\/code><\/pre>\n\n\n\n<p>Segment\u7ee7\u627f\u4e86ReentrantLock\uff0c\u8868\u660e\u6bcf\u4e2asegment\u3010\u672c\u8eab\u3011\u90fd\u53ef\u4ee5\u5f53\u505a\u4e00\u4e2a\u9501\u3002\uff08ReentrantLock\u524d\u6587\u5df2\u7ecf\u63d0\u5230\uff0c\u4e0d\u4e86\u89e3\u7684\u8bdd\u5c31\u628a\u5f53\u505asynchronized\u7684\u66ff\u4ee3\u8005\u5427\uff09\u8fd9\u6837\u5bf9\u6bcf\u4e2asegment\u4e2d\u7684\u6570\u636e\u9700\u8981\u540c\u6b65\u64cd\u4f5c\u7684\u8bdd\u90fd\u662f\u4f7f\u7528\u6bcf\u4e2asegment\u5bb9\u5668\u5bf9\u8c61\u81ea\u8eab\u7684\u9501\u6765\u5b9e\u73b0\u3002\u53ea\u6709\u5bf9\u5168\u5c40\u9700\u8981\u6539\u53d8\u65f6\u9501\u5b9a\u7684\u662f\u6240\u6709\u7684segment\u3002Segment\u4e0b\u9762\u5305\u542b\u5f88\u591a\u4e2aHashEntry\u5217\u8868\u6570\u7ec4\u3002\u5bf9\u4e8e\u4e00\u4e2akey\uff0c\u9700\u8981\u7ecf\u8fc7\u4e09\u6b21\uff08\u4e3a\u4ec0\u4e48\u8981hash\u4e09\u6b21\u4e0b\u6587\u4f1a\u8be6\u7ec6\u8bb2\u89e3\uff09hash\u64cd\u4f5c\uff0c\u624d\u80fd\u6700\u7ec8\u5b9a\u4f4d\u8fd9\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u8fd9\u4e09\u6b21hash\u5206\u522b\u4e3a\uff1a<\/p>\n\n\n\n<p class=\"has-small-font-size\">1\uff0c\u5bf9\u4e8e\u4e00\u4e2akey\uff0c\u5148\u8fdb\u884c\u4e00\u6b21hash\u64cd\u4f5c\uff0c\u5f97\u5230hash\u503ch1\uff0c\u4e5f\u5373h1 = hash1(key)\uff1b<br>2\uff0c\u5c06\u5f97\u5230\u7684h1\u7684\u9ad8\u51e0\u4f4d\u8fdb\u884c\u7b2c\u4e8c\u6b21hash\uff0c\u5f97\u5230hash\u503ch2\uff0c\u4e5f\u5373h2 = hash2(h1\u9ad8\u51e0\u4f4d)\uff0c\u901a\u8fc7h2\u80fd\u591f\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u653e\u5728\u54ea\u4e2aSegment\uff1b<br>3\uff0c\u5c06\u5f97\u5230\u7684h1\u8fdb\u884c\u7b2c\u4e09\u6b21hash\uff0c\u5f97\u5230hash\u503ch3\uff0c\u4e5f\u5373h3 = hash3(h1)\uff0c\u901a\u8fc7h3\u80fd\u591f\u786e\u5b9a\u8be5\u5143\u7d20\u653e\u7f6e\u5728\u54ea\u4e2aHashEntry\u3002ConcurrentHashMap\u4e2d\u4e3b\u8981\u5b9e\u4f53\u7c7b\u5c31\u662f\u4e09\u4e2a\uff1aConcurrentHashMap\uff08\u6574\u4e2aHash\u8868\uff09,Segment\uff08\u6876\uff09\uff0cHashEntry\uff08\u8282\u70b9\uff09\uff0c\u5bf9\u5e94\u4e0a\u9762\u7684\u56fe\u53ef\u4ee5\u770b\u51fa\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u56db\u90e8\u5206\uff1a\u7ebf\u7a0b\u6c60\u76f8\u5173<\/strong><\/h3>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\">1. Executors \u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u51e0\u4e2a\u65b9\u6cd5<\/h5>\n\n\n\n<ul>\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">newSingleThreadExecutor<\/mark><\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>new ThreadPoolExecutor(1, \n                       1, \n                       0L, \n                       TimeUnit.MILLISECONDS, \n                       new LinkedBlockingQueue&lt;Runnable&gt;(), \n                       threadFactory));<\/code><\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u521b\u5efa\u4e00\u4e2a\u5355\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\u3002<\/mark>\u5b83\u53ea\u4f1a\u7528\u552f\u4e00\u7684\u5de5\u4f5c\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff0c\u4fdd\u8bc1\u6240\u6709\u4efb\u52a1\u6309\u7167\u6307\u5b9a\u987a\u5e8f(FIFO, LIFO, \u4f18\u5148\u7ea7)\u6267\u884c\u3002\u8fd9\u4e2a\u7ebf\u7a0b\u6c60\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u5de5\u4f5c\uff0c\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e\u5355\u7ebf\u7a0b\u4e32\u884c\u6267\u884c\u6240\u6709\u4efb\u52a1\u3002\u5982\u679c\u8fd9\u4e2a\u552f\u4e00\u7684\u7ebf\u7a0b\u56e0\u4e3a\u5f02\u5e38\u7ed3\u675f\uff0c\u90a3\u4e48\u4f1a\u6709\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u6765\u66ff\u4ee3\u5b83\u3002\u6b64\u7ebf\u7a0b\u6c60\u4fdd\u8bc1\u6240\u6709\u4efb\u52a1\u7684\u6267\u884c\u987a\u5e8f\u6309\u7167\u4efb\u52a1\u7684\u63d0\u4ea4\u987a\u5e8f\u6267\u884c\u3002\u3010\u6ce8\u610f\u8fd9\u4e2a\u65b9\u6cd5\u8c03\u7528\u7684ThreadPoolExecutor\u91cc\u521d\u59cb\u7ebf\u7a0b\u6570\u91cf\u662f1\uff0c\u6700\u5927\u7ebf\u7a0b\u6570\u91cf\u4e5f\u662f1\uff0c\u4e5f\u5c31\u662f\u8bf4 <strong>newSingleThreadExecutor<\/strong> \u521b\u5efa\u7684\u7ebf\u7a0b\u6c60\u91cc\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u3002==\u300b\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u4f46\u662f\u91cc\u9762\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u76ee\u7684\u662f\u4ec0\u4e48\u5462\uff1f\u662f\u8ba9\u4efb\u52a1\u987a\u5e8f\u6267\u884c\u3002\u3011<\/p>\n\n\n\n<ul>\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">newFixedThreadPool<\/mark><\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>new ThreadPoolExecutor(n Threads,\n                       n Threads, \n                       0L,\n                       TimeUnit.MILLISECONDS,\n                       new LinkedBlockingQueue&lt;Runnable&gt;());<\/code><\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u521b\u5efa\u56fa\u5b9a\u5927\u5c0f\u7684\u7ebf\u7a0b\u6c60\u3002<\/mark>\u53ef\u63a7\u5236\u7ebf\u7a0b\u6700\u5927\u5e76\u53d1\u6570\uff0c\u8d85\u51fa\u7684\u7ebf\u7a0b\u4f1a\u5728\u961f\u5217\u4e2d\u7b49\u5f85\u3002\u6bcf\u6b21\u63d0\u4ea4\u4e00\u4e2a\u4efb\u52a1\u5c31\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u76f4\u5230\u7ebf\u7a0b\u8fbe\u5230\u7ebf\u7a0b\u6c60\u7684\u6700\u5927\u5927\u5c0f\u3002\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\u4e00\u65e6\u8fbe\u5230\u6700\u5927\u503c\u5c31\u4f1a\u4fdd\u6301\u4e0d\u53d8\uff0c\u5982\u679c\u67d0\u4e2a\u7ebf\u7a0b\u56e0\u4e3a\u6267\u884c\u5f02\u5e38\u800c\u7ed3\u675f\uff0c\u90a3\u4e48\u7ebf\u7a0b\u6c60\u4f1a\u8865\u5145\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u3002\u3010\u8fd9\u79cd\u7ebf\u7a0b\u6c60\u9002\u7528\u4e8e\u6211\u4eec\u4e8b\u5148\u77e5\u9053\u81ea\u5df1\u7684\u4e1a\u52a1\u91cf\u591a\u5c11\u548c\u5bf9\u7cfb\u7edf\u7684\u538b\u529b\u591a\u5c11, \u56e0\u4e3a\u8fd9\u79cd\u7ebf\u7a0b\u6c60\u7684\u4efb\u52a1\u961f\u5217\u662fLinkedBlockingQueue, \u53ef\u4ee5\u4efb\u52a1\u662f\u65e0\u9650\u5927\u5c0f\u7684\u961f\u5217, \u5f53\u4efb\u52a1\u91cf\u5f88\u591a\u7684\u65f6\u5019\u4f1a\u5bf9\u7cfb\u7edf\u9020\u6210\u5f88\u5927\u7684\u538b\u529b\u3011<\/p>\n\n\n\n<ul>\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">newCachedThreadPool<\/mark><\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>new ThreadPoolExecutor(0,\n                       Integer.MAX_VALUE,\n                       60L,\n                       TimeUnit.SECONDS,\n                       new SynchronousQueue&lt;Runnable&gt;());<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u53ef\u7f13\u5b58\u7684\u7ebf\u7a0b\u6c60\u3002\u5982\u679c\u7ebf\u7a0b\u6c60\u957f\u5ea6\u8d85\u8fc7\u5904\u7406\u9700\u8981\uff0c\u53ef\u7075\u6d3b\u56de\u6536\u7a7a\u95f2\u7ebf\u7a0b\uff0c\u82e5\u65e0\u53ef\u56de\u6536\uff0c\u5219\u65b0\u5efa\u7ebf\u7a0b\u3002<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5982\u679c\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\u8d85\u8fc7\u4e86\u5904\u7406\u4efb\u52a1\u6240\u9700\u8981\u7684\u7ebf\u7a0b\uff0c\u90a3\u4e48\u5c31\u4f1a\u56de\u6536\u90e8\u5206\u7a7a\u95f2\uff0860\u79d2\u4e0d\u6267\u884c\u4efb\u52a1\uff09\u7684\u7ebf\u7a0b\uff0c\u5f53\u4efb\u52a1\u6570\u589e\u52a0\u65f6\uff0c\u6b64\u7ebf\u7a0b\u6c60\u53c8\u53ef\u4ee5\u667a\u80fd\u7684\u6dfb\u52a0\u65b0\u7ebf\u7a0b\u6765\u5904\u7406\u4efb\u52a1\u3002\u6b64\u7ebf\u7a0b\u6c60\u4e0d\u4f1a\u5bf9\u7ebf\u7a0b\u6c60\u5927\u5c0f\u505a\u9650\u5236\uff0c<\/mark>\u7ebf\u7a0b\u6c60\u5927\u5c0f\u5b8c\u5168\u4f9d\u8d56\u4e8e\u64cd\u4f5c\u7cfb\u7edf\uff08\u6216\u8005\u8bf4JVM\uff09\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7ebf\u7a0b\u5927\u5c0f\u3002<\/p>\n\n\n\n<ul>\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">newScheduledThreadPool<\/mark><\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>super(corePoolSize, \n      Integer.MAX_VALUE,\n      0,\n      TimeUnit.NANOSECONDS,\n      new DelayedWorkQueue());<\/code><\/pre>\n\n\n\n<p>\u7ee7\u7eed\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this(corePoolSize,\n     maximumPoolSize,\n     keepAliveTime,\n     unit,\n     workQueue,\n     Executors.defaultThreadFactory(),\n     defaultHandler);<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u65e0\u9650\u7684\u7ebf\u7a0b\u6c60\u3002\u652f\u6301\u5b9a\u65f6\u53ca\u5468\u671f\u6027\u4efb\u52a1\u6267\u884c\u3002<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6b64\u7ebf\u7a0b\u6c60\u652f\u6301\u5b9a\u65f6\u4ee5\u53ca\u5468\u671f\u6027\u6267\u884c\u4efb\u52a1\u7684\u9700\u6c42\u3002<\/mark><\/p>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\">2. \u7ebf\u7a0b\u6c60\u6838\u5fc3\u53c2\u6570<\/h5>\n\n\n\n<p>\u5728\u4ecb\u7ecd\u4f7f\u7528 <strong>Executors <\/strong>\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5\u524d\uff0c\u5148\u4ecb\u7ecd\u4e00\u4e0b ThreadPoolExecutor \uff0c\u56e0\u4e3a\u8fd9\u4e9b\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u9759\u6001\u65b9\u6cd5\u90fd\u662f\u8fd4\u56deThreadPoolExecutor\u5bf9\u8c61\uff0c\u548c\u6211\u4eec\u624b\u52a8\u521b\u5efa ThreadPoolExecutor \u5bf9\u8c61\u7684\u533a\u522b\u5c31\u662f\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u4f20\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<p>ThreadPoolExecutor \u7684\u6784\u9020\u51fd\u6570\u5171\u6709\u56db\u4e2a\uff0c\u4f46\u6700\u7ec8\u8c03\u7528\u7684\u90fd\u662f\u540c\u4e00\u4e2a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public ThreadPoolExecutor(\n                int corePoolSize, \n                int maximumPoolSize, \n                long keepAliveTime, \n                TimeUnit unit, \n                BlockingQueue workQueue, \n                ThreadFactory threadFactory, \n                RejectedExecutionHandler handler)<\/code><\/pre>\n\n\n\n<p><strong>\u6784\u9020\u51fd\u6570\u53c2\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"has-small-font-size\">\n<li><strong>corePoolSize<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u6838\u5fc3\u7ebf\u7a0b\u6570\u91cf ===\u300b\u300b\u3010\u5f53\u6838\u5fc3\u7ebf\u7a0b\u672a\u6ee1\u65f6\u65b0\u6765\u4efb\u52a1\u4f1a\u521b\u5efa\u6838\u5fc3\u7ebf\u7a0b\uff0c\u5f53corePoolSize\u6ee1\u4e86\u4ee5\u540e\uff0c\u4efb\u52a1\u4f1a\u8fdb\u5165\u963b\u585e\u961f\u5217\uff0c\u53ea\u6709\u5f53\u963b\u585e\u961f\u5217\u6ee1\u4e86\u4ee5\u540e\uff0c\u624d\u4f1a\u521b\u5efa\u975e\u6838\u5fc3\u7ebf\u7a0b\uff0c\u5f53\u6700\u5927\u7ebf\u7a0b\u6570\u4e0e\u961f\u5217\u5747\u6ee1\u4e86\u4ee5\u540e\uff0c\u624d\u4f1a\u6267\u884c\u62d2\u7edd\u7b56\u7565\u3002\u3011<\/li>\n\n\n\n<li><strong>maximumPoolSize <\/strong>\uff1a\u7ebf\u7a0b\u6c60\u6700\u5927\u6570\u91cf<\/li>\n\n\n\n<li><strong>keepAliveTime<\/strong>\uff1a\u7a7a\u95f2\u7ebf\u7a0b\u5b58\u6d3b\u65f6\u95f4 ===\u300b\u300b\u8fd9\u4e2a\u53c2\u6570\u975e\u5e38\u91cd\u8981\u3002\u3010\u5f53\u6c60\u4e2d\u7ebf\u7a0b\u6570\u5927\u4e8e\u6838\u5fc3\u7ebf\u7a0b\u6570\u65f6\uff0c\u8be5\u65f6\u95f4\u4e3a\u4f59\u4e0b\u7ebf\u7a0b\uff08\u5b58\u6d3b\u7ebf\u7a0b\u603b\u6570-\u6838\u5fc3\u7ebf\u7a0b\u6570\uff09\u7684\u6700\u5927\u7a7a\u95f2\u5b58\u6d3b\u65f6\u95f4\u3011<\/li>\n\n\n\n<li>unit =&gt; \u65f6\u95f4\u5355\u4f4d<\/li>\n\n\n\n<li><strong>workQueue<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u6240\u4f7f\u7528\u7684\u7f13\u51b2\u961f\u5217<\/li>\n\n\n\n<li><strong>threadFactory<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u521b\u5efa\u7ebf\u7a0b\u4f7f\u7528\u7684\u5de5\u5382\u3010\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u5c31\u662f\u5728\u8fd9\u521b\u5efa\u7684\u3011<\/li>\n\n\n\n<li><strong>handler<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u5bf9\u62d2\u7edd\u4efb\u52a1\u7684\u5904\u7406\u7b56\u7565 ===\u300b\u300b \u62d2\u7edd\u7b56\u7565\u4e5f\u5f88\u91cd\u8981, \u9ed8\u8ba4\u7684\u62d2\u7edd\u7b56\u7565\u662f\u629b\u5f02\u5e38, \u4f46\u662f\u6709\u7684\u4e1a\u52a1\u4f60\u9700\u8981\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u7684, \u56e0\u4e3a\u5426\u5219\u4f1a\u51fa\u73b0\u6570\u636e\u95ee\u9898, \u6240\u4ee5\u9ed8\u8ba4\u7684\u62d2\u7edd\u7b56\u7565\u5c31\u4e0d\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6267\u884c\u903b\u8f91\u8bf4\u660e\uff1a<\/strong><br>\u5224\u65ad\u6838\u5fc3\u7ebf\u7a0b\u6570\u662f\u5426\u5df2\u6ee1\uff0c\u6838\u5fc3\u7ebf\u7a0b\u6570\u5927\u5c0f\u548ccorePoolSize\u53c2\u6570\u6709\u5173\uff0c\u672a\u6ee1\u5219\u521b\u5efa\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\uff0c\u82e5\u6838\u5fc3\u7ebf\u7a0b\u6c60\u5df2\u6ee1\uff0c\u5224\u65ad\u961f\u5217\u662f\u5426\u6ee1\uff0c\u961f\u5217\u662f\u5426\u6ee1\u548cworkQueue\u53c2\u6570\u6709\u5173\uff0c\u82e5\u672a\u6ee1\u5219\u52a0\u5165\u961f\u5217\u4e2d\uff0c\u82e5\u961f\u5217\u5df2\u6ee1\uff0c\u5224\u65ad\u7ebf\u7a0b\u6c60\u662f\u5426\u5df2\u6ee1\uff0c\u7ebf\u7a0b\u6c60\u662f\u5426\u5df2\u6ee1\u548cmaximumPoolSize\u53c2\u6570\u6709\u5173\uff0c\u82e5\u672a\u6ee1\u521b\u5efa\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\uff0c\u82e5\u7ebf\u7a0b\u6c60\u5df2\u6ee1\uff0c\u5219\u91c7\u7528\u62d2\u7edd\u7b56\u7565\u5904\u7406\u65e0\u6cd5\u6267\u6267\u884c\u7684\u4efb\u52a1\uff0c\u62d2\u7edd\u7b56\u7565\u548chandler\u53c2\u6570\u6709\u5173\u3002<\/p>\n<\/blockquote>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\">3. \u7ebf\u7a0b\u72b6\u6001<\/h5>\n\n\n\n<p>\u53c2\u7167 Thread \u7c7b\u5185\u90e8\u6e90\u7801\u72b6\u6001\u5b9a\u4e49\uff1ajava.lang.Thread.State<\/p>\n\n\n\n<ol>\n<li><mark><strong>New<\/strong><\/mark>\uff1a\u5c1a\u672a\u542f\u52a8\u7684\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u72b6\u6001\u3002<strong>\u3010\u521anew\u7684\u7ebf\u7a0b\uff0c\u8fd8\u6ca1\u6709\u8c03\u7528 start \u65b9\u6cd5\u3011<\/strong><\/li>\n\n\n\n<li><strong><mark>Runnable<\/mark><\/strong>\uff1a\u53ef\u8fd0\u884c\/\u8fd0\u884c\u4e2d\u7ebf\u7a0b\u7684\u72b6\u6001\uff0cjava\u6ca1\u5bf9\u201c\u53ef\u8fd0\u884c\/\u8fd0\u884c\u4e2d\u201d\u7684\u7ebf\u7a0b\u72b6\u6001\u505a\u7ec6\u5206\uff0c\u6240\u4ee5\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528\u4e86start\u65b9\u6cd5\uff0c\u5c31\u662fRunnable\u72b6\u6001\uff0c\u4f46\u7ebf\u7a0b\u771f\u6b63\u7684\u72b6\u6001\u53ef\u80fd\u662f\u7b49\u5f85CPU\u8c03\u5ea6\u3001\u6216\u8005\u6b63\u5728\u6267\u884c\u4e2d\u3002<\/li>\n\n\n\n<li><strong><mark>Blocked<\/mark><\/strong>\uff1a\u7ebf\u7a0b\u963b\u585e\u7b49\u5f85\u76d1\u89c6\u5668\u9501\u5b9a\u7684\u7ebf\u7a0b\u72b6\u6001\u3002\u5904\u4e8esynchronized\u540c\u6b65\u4ee3\u7801\u5757\u6216\u65b9\u6cd5\u4e2d\u88ab\u963b\u585e\u3002<strong>\u3010\u963b\u585e\u72b6\u6001\uff0c\u5373\u83b7\u53d6\u9501\u800c\u4e0d\u53ef\u5f97\u7684\u72b6\u6001\uff0c\u5b98\u7f51\u6587\u6863\u4e0a\u8bf4\u8be5\u72b6\u6001\u662f\u7b49\u5f85\u76d1\u89c6\u5668\u9501\u5b9a\u7684\u72b6\u6001\u3002\u3011<\/strong><\/li>\n\n\n\n<li><strong><mark>Waiting<\/mark><\/strong>\uff1a\u7b49\u5f85\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u72b6\u6001\u3002\u4e0b\u5217\u4e0d\u5e26\u8d85\u65f6\u7684\u65b9\u5f0f\uff1a<strong><em>\u65e0\u53c2\u7684Object.wait\u3001\u65e0\u53c2\u7684Thread.join\u3001\u65e0\u53c2\u7684LockSupport.park<\/em><\/strong>=\u300b\u5b98\u65b9\u6587\u6863\u5c31\u662f\u8bf4\u88ab\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u8c03\u7528\u7684\u65f6\u5019\u4f1a\u5904\u4e8eWaiting\u72b6\u6001<strong>\u3010waiting\u72b6\u6001\uff0c\u7ebf\u7a0b\u81ea\u5df1\u91ca\u653eCPU\u548c\u9501\u3011<\/strong><\/li>\n\n\n\n<li><strong><mark>Timed Waiting<\/mark><\/strong>\uff1a\u5177\u6709\u6307\u5b9a\u7b49\u5f85\u65f6\u95f4\u7684\u7b49\u5f85\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u72b6\u6001\u3002\u4e0b\u5217\u8d85\u65f6\u7684\u65b9\u5f0f\uff1a<strong><em>Thread.sleep\u3001\u5e26\u53c2\u7684Object.wait\u3001\u5e26\u53c2\u7684Thread.join\u3001LockSupport.parkNanos\u3001LockSupport.parkUntil<\/em><\/strong>=\u300b\u5b98\u65b9\u6587\u6863\u5c31\u662f\u8bf4\u88ab\u8fd9\u4e94\u4e2a\u65b9\u6cd5\u8c03\u7528\u7684\u65f6\u5019\u4f1a\u5904\u4e8eTimed Waiting\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong><mark>Terminated<\/mark><\/strong>\uff1a\u7ebf\u7a0b\u7ec8\u6b62\u72b6\u6001\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4ee5\u4e0a\u662f Thread \u7c7b\u5185\u90e8\u6e90\u7801\u5b9a\u4e49\u7684\u7ebf\u7a0b\u76846\u4e2a\u72b6\u6001\u3002<strong>\u6309\u7167\u8fd9\u4e2a\u5b9a\u4e49\uff0c\u5176\u5b9e6\u79cd\u72b6\u6001\u63cf\u8ff0\u7684\u975e\u5e38\u6e05\u6670\uff0c\u4f46\u662f\u5f88\u591a\u6280\u672f\u535a\u5ba2\u3001\u6587\u7ae0\u90fd\u5c06\u201cBlocked\u963b\u585e\u72b6\u6001\u3001Waiting\u72b6\u6001\u3001Timed Waiting\u72b6\u6001\u201d\u6df7\u4e3a\u4e00\u8c08\u4e86\uff0c\u628aWaiting\u72b6\u6001\u3001Timed Waiting\u72b6\u6001\u90fd\u79f0\u4e3a\u201c\u963b\u585e\u72b6\u6001\u201d\u3002<\/strong><\/p>\n\n\n\n<p>\u5982\u4e0a\uff0c\u5176\u5b9e\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u540c\u4e00\u79cd\u72b6\u6001\uff0c\u53ef\u80fd\u662f\u7531\u4e0d\u540c\u7684\u51fd\u6570\u5bfc\u81f4\u7684<\/strong>\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul class=\"has-small-font-size\">\n<li>Waiting\u72b6\u6001\uff1a\u53ef\u80fd\u662fwait()\u65b9\u6cd5\u3001join\u65b9\u6cd5\u5bfc\u81f4\u7684\u3002<\/li>\n\n\n\n<li>Timed Waiting\u72b6\u6001\uff1a\u53ef\u80fd\u662fsleep\u3001wait(\u6beb\u79d2)\u7b49\u5bfc\u81f4\u7684\uff0c\u4f46sleep\u65b9\u6cd5\u4e0d\u4f1a\u5bfc\u81f4\u5f53\u524d\u7ebf\u7a0b\u91ca\u653e\u9501\uff0c\u800cwait\u65b9\u6cd5\u4f1a\u3002<\/li>\n\n\n\n<li>Blocked\u963b\u585e\u72b6\u6001\uff1a\u53ea\u6709\u5728\u201c\u83b7\u53d6\u9501\u800c\u4e0d\u53ef\u5f97\u201d\u65f6\uff0c\u624d\u79f0\u4e3a\u963b\u585e\u72b6\u6001\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-color\">\u5173\u4e8e\u201c\u4e00\u4e2a\u7ebf\u7a0b\u6682\u505c\u6267\u884c\u4e86\u201d\uff0c\u6211\u770b\u7f51\u4e0a\u6709\u591a\u79cd\u63cf\u8ff0\uff0c\u6bd4\u5982\u963b\u585e\u3001\u7b49\u5f85\u3001\u7761\u7720\u3001\u6302\u8d77\uff0c\u7b49\u7b49\uff0c\u975e\u5e38\u4e71\u4e14\u6613\u6df7\u6dc6\u3002\u6211\u5728\u6b64\u91cd\u65b0\u5bf9\u8fd9\u4e9b\u540d\u8bcd\u68b3\u7406\u4e0b\u3002<\/mark><\/strong>\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u4e2d<strong>\u6682\u505c\u6267\u884c<\/strong>\u4e86\uff0c\u6709\u4e0b\u9762\u8fd9\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ol class=\"has-small-font-size\">\n<li>\u963b\u585e\u72b6\u6001\uff08\u83b7\u53d6\u9501\u800c\u4e0d\u53ef\u5f97\uff0c\u4f7f\u7ebf\u7a0b<strong>\u6682\u505c\u6267\u884c<\/strong>\uff0c<s>\u8be5\u7ebf\u7a0b\u4e0d\u4f1a\u91ca\u653e\u5df2\u6709\u7684\u9501\u548cCPU<\/s>==\u300b\u9519\uff0c\u963b\u585e\u72b6\u6001\u7684\u7ebf\u7a0b\u5c31\u6ca1\u6709\u83b7\u53d6\u5230\u9501\u554a\uff09\u3002<\/li>\n\n\n\n<li>\u7b49\u5f85\u72b6\u6001\uff08\u8c03\u7528\u65e0\u53c2\u7684wait\u3001join\u3001park\u7b49\u51fd\u6570\uff0c\u4f7f\u7ebf\u7a0b<strong>\u6682\u505c\u6267\u884c<\/strong>\uff0c\u91ca\u653e\u9501\u548cCPU\uff09\u3002<\/li>\n\n\n\n<li>\u9650\u65f6\u7b49\u5f85\u72b6\u6001\uff08\u8c03\u7528sleep\u51fd\u6570\u3001\u6709\u53c2\u7684wait\u3001join\u7b495\u4e2a\u65b9\u6cd5\uff0c\u4f7f\u7ebf\u7a0b<strong>\u6682\u505c\u6267\u884c<\/strong>\uff0c\u5177\u4f53\u91ca\u653e\u4e0d\u91ca\u653e\u9501\uff0c\u4e0d\u540c\u51fd\u6570\u4e0d\u540c\u60c5\u51b5\u3002\u6bd4\u5982sleep\u51fd\u6570\u4e0d\u91ca\u653e\u9501\u548cCPU=\u300b\u201c\u4e0d\u91ca\u653eCPU\u201d\u8fd9\u4e2a\u63cf\u8ff0\u4e0d\u51c6\u786e\uff0c\u5982\u679c\u4e0d\u91ca\u653eCPU\u5219\u5176\u4ed6\u7ebf\u7a0b\u5c31\u83b7\u53d6\u4e0d\u5230CPU\u4e86\uff0c\u51c6\u786e\u8bf4\u662f\u4e0d\u91ca\u653e\u9501\uff0c\u4f46\u7761\u7720\u65f6\u95f4\u4e00\u8fc7\u5c31\u53ef\u4ee5\u83b7\u53d6CPU\u6267\u884c\uff09<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u6302\u8d77\uff08\u8c03\u7528suspend\u51fd\u6570\uff0csuspend()\u4f7f\u5f97\u7ebf\u7a0b\u8fdb\u5165<strong>\u6682\u505c\u6267\u884c\uff08<\/strong>\u4e0d\u4f1a\u91ca\u653e\u9501<strong>\uff09<\/strong>\uff0c\u5e76\u4e14\u4e0d\u4f1a\u81ea\u52a8\u6062\u590d\uff0c\u5fc5\u987b\u5176\u5bf9\u5e94\u7684resume() \u88ab\u8c03\u7528\uff0c\u624d\u80fd\u4f7f\u5f97\u7ebf\u7a0b\u91cd\u65b0\u8fdb\u5165\u53ef\u6267\u884c\u72b6\u6001\u3002\u8fd9\u4e24\u4e2a\u662f\u8fc7\u671f\u65b9\u6cd5\u3002\uff09<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e94\u90e8\u5206\uff1a\u7ebf\u7a0b\u5b89\u5168<\/h3>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\">1. ThreadLocal \u539f\u7406<\/h5>\n\n\n\n<p>ThreadLocal \u7684\u539f\u7406\uff0c\u89c1\u6211\u7684\u6587\u7ae0 <a href=\"http:\/\/codermr.com\/index.php\/2023\/03\/25\/threadlocal\/\">ThreadLocal \u539f\u7406\u5206\u6790<\/a> \u3002<\/p>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\">2. volatile<\/h5>\n\n\n\n<p>\u53c2\u7b14\u8bb0\uff1a<a href=\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\" data-type=\"URL\" data-id=\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\">volatile \u8be6\u89e3<\/a><\/p>\n\n\n\n<h5 class=\"has-normal-font-size wp-block-heading\">3. AtomicInteger<\/h5>\n\n\n\n<p>AtomicInteger \u91cc\u9762\u5176\u5b9e\u4f7f\u7528\u7684 CAS \u539f\u7406\uff0c\u6700\u7ec8\u8c03\u7528\u7684 unSafe \u7684 CAS \u65b9\u6cd5\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">4. \u4ec0\u4e48\u662f\u53ef\u91cd\u5165\u9501\uff1fsynchroniized ? ReentrantLock ?<\/h5>\n\n\n\n<p>\u53ef\u91cd\u5165\u9501\uff0c\u6307\u9501\u53ef\u4ee5\u88ab\u5355\u4e2a\u7ebf\u7a0b\u591a\u6b21\u83b7\u53d6\u3002Synchronized\u548cReentrantLock\u90fd\u662f\u53ef\u91cd\u5165\u9501\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"331\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-6-1024x331.png?v=1675345552\" alt=\"\" class=\"wp-image-312\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-6-1024x331.png 1024w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-6-300x97.png 300w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-6-768x248.png 768w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-6-1536x496.png 1536w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-6-2048x662.png 2048w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/image-6-816x264.png 816w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>ReentrantLock \u548c synchronized \u90fd\u662f\u53ef\u91cd\u5165\u9501\u3002\u800c LockSupport.park \u5219\u662f\u4e0d\u53ef\u91cd\u5165\u7684\uff0c\u8fde\u7eed\u8c03\u7528\u4e24\u6b21 LockSupport.park \u5219\u8fd9\u4e2a\u7ebf\u7a0b\u5c31\u4f1a\u963b\u585e\u4e86\u3002<\/p>\n\n\n\n<p>park\/unpark \u6a21\u578b\u771f\u6b63\u89e3\u8026\u4e86\u7ebf\u7a0b\u4e4b\u95f4\u7684\u540c\u6b65\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u4e0d\u518d\u9700\u8981\u4e00\u4e2a Object \u6216\u8005\u5176\u5b83\u53d8\u91cf\u6765\u5b58\u50a8\u72b6\u6001\uff0c\u4e0d\u518d\u9700\u8981\u5173\u5fc3\u5bf9\u65b9\u7684\u72b6\u6001\u3002Synchronized \u7684\u963b\u585e\u65e0\u6cd5\u88ab\u4e2d\u65ad\uff0c\u800c ReentrantLock \u5219\u63d0\u4f9b\u4e86\u53ef\u4e2d\u65ad\u7684\u963b\u585e\u3002<\/p>\n\n\n\n<p>synchronized \u5173\u952e\u5b57\u662f\u4e0d\u80fd\u7ee7\u627f\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u57fa\u7c7b\u7684\u65b9\u6cd5 synchronized f(){} \u5728\u7ee7\u627f\u7c7b\u4e2d\u5e76\u4e0d\u81ea\u52a8\u662f synchronized f(){}\uff0c\u800c\u662f\u53d8\u6210\u4e86f(){}\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">5. \u4ec0\u4e48\u662f\u6b7b\u9501\uff1f\u600e\u4e48\u907f\u514d\u6b7b\u9501\u7684\u53d1\u751f\uff1f<\/h5>\n\n\n\n<p>\u5f53\u4e24\u4e2a\u7ebf\u7a0b\u4e3a\u4e86\u4fdd\u62a4\u4e24\u4e2a\u4e0d\u540c\u7684\u5171\u4eab\u8d44\u6e90\u800c\u4f7f\u7528\u4e86\u4e24\u4e2a\u4e92\u65a5\u9501\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u4e92\u65a5\u9501\u5e94\u7528\u4e0d\u5f53\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u9501\uff0c\u5728\u6ca1\u6709\u5916\u529b\u7684\u4f5c\u7528\u4e0b\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u76f8\u4e92\u7b49\u5f85\uff0c\u5c31\u6ca1\u529e\u6cd5\u7ee7\u7eed\u8fd0\u884c\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u662f\u53d1\u751f\u4e86\u6b7b\u9501\u3002<\/p>\n\n\n\n<p>\u6b7b\u9501\u53ea\u6709\u540c\u65f6\u6ee1\u8db3\u4ee5\u4e0b\u56db\u4e2a\u6761\u4ef6\u624d\u4f1a\u53d1\u751f\uff1a<\/p>\n\n\n\n<ul>\n<li>\u4e92\u65a5\u6761\u4ef6\uff08\u5373\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u80fd\u540c\u65f6\u4f7f\u7528\u540c\u4e00\u4e2a\u8d44\u6e90\uff09<\/li>\n\n\n\n<li>\u6301\u6709\u5e76\u7b49\u5f85\u6761\u4ef6\uff08\u5f53\u7ebf\u7a0b A \u5df2\u7ecf\u6301\u6709\u4e86\u8d44\u6e90 1\uff0c\u53c8\u60f3\u7533\u8bf7\u8d44\u6e90 2\uff0c\u800c\u8d44\u6e90 2 \u5df2\u7ecf\u88ab\u7ebf\u7a0b C \u6301\u6709\u4e86\uff0c\u6240\u4ee5\u7ebf\u7a0b A \u5c31\u4f1a\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\uff0c\u4f46\u662f\u7ebf\u7a0b A \u5728\u7b49\u5f85\u8d44\u6e90 2 \u7684\u540c\u65f6\u5e76\u4e0d\u4f1a\u91ca\u653e\u81ea\u5df1\u5df2\u7ecf\u6301\u6709\u7684\u8d44\u6e90 1 \uff09<\/li>\n\n\n\n<li>\u4e0d\u53ef\u5265\u593a\u6761\u4ef6\uff08\u5f53\u7ebf\u7a0b\u5df2\u7ecf\u6301\u6709\u4e86\u8d44\u6e90 \uff0c\u5728\u81ea\u5df1\u4f7f\u7528\u5b8c\u4e4b\u524d\u4e0d\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u83b7\u53d6\u3002\uff09<\/li>\n\n\n\n<li>\u73af\u8def\u7b49\u5f85\u6761\u4ef6\uff08\u5728\u6b7b\u9501\u53d1\u751f\u7684\u65f6\u5019\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u8d44\u6e90\u7684\u987a\u5e8f\u6784\u6210\u4e86\u73af\u5f62\u94fe\uff09<\/li>\n<\/ul>\n\n\n\n<p><strong>\u600e\u4e48\u68c0\u6d4b\u6b7b\u9501\uff1f<\/strong><\/p>\n\n\n\n<p>jstack \u662f java \u865a\u62df\u673a\u81ea\u5e26\u7684\u4e00\u79cd\u5806\u6808\u8ddf\u8e2a\u5de5\u5177\u3002jstack \u7528\u4e8e\u6253\u5370\u51fa\u7ed9\u5b9a\u7684 java \u8fdb\u7a0b ID \u6216 core file \u6216\u8fdc\u7a0b\u8c03\u8bd5\u670d\u52a1\u7684 Java \u5806\u6808\u4fe1\u606f\u3002jstack \u4e3b\u8981\u7528\u4e8e\u751f\u6210 java \u865a\u62df\u673a\u5f53\u524d\u65f6\u523b\u7684\u7ebf\u7a0b\u5feb\u7167\uff0c\u7ebf\u7a0b\u5feb\u7167\u662f\u5f53\u524d java \u865a\u62df\u673a\u5185\u6bcf\u4e00\u6761\u7ebf\u7a0b\u6b63\u5728\u6267\u884c\u7684\u65b9\u6cd5\u5806\u6808\u7684\u96c6\u5408\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u751f\u6210\u7ebf\u7a0b\u5feb\u7167\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5b9a\u4f4d\u7ebf\u7a0b\u51fa\u73b0\u957f\u65f6\u95f4\u505c\u987f\u7684\u539f\u56e0\uff0c\u5982\u7ebf\u7a0b\u95f4\u6b7b\u9501\u3001\u6b7b\u5faa\u73af\u3001\u8bf7\u6c42\u5916\u90e8\u8d44\u6e90\u5bfc\u81f4\u7684\u957f\u65f6\u95f4\u7b49\u5f85\u7b49<\/mark>\u3002 \u7ebf\u7a0b\u51fa\u73b0\u505c\u987f\u7684\u65f6\u5019\u901a\u8fc7 jstack \u6765\u67e5\u770b\u5404\u4e2a\u7ebf\u7a0b\u7684\u8c03\u7528\u5806\u6808\uff0c\u5c31\u53ef\u4ee5\u77e5\u9053\u6ca1\u6709\u54cd\u5e94\u7684\u7ebf\u7a0b\u5230\u5e95\u5728\u540e\u53f0\u505a\u4ec0\u4e48\u4e8b\u60c5\uff0c\u6216\u8005\u7b49\u5f85\u4ec0\u4e48\u8d44\u6e90\u3002<\/p>\n\n\n\n<p><strong>\u600e\u4e48\u907f\u514d\u6b7b\u9501\u7684\u53d1\u751f\uff1f<\/strong><\/p>\n\n\n\n<p>\u56e0\u4e3a\u5df2\u7ecf\u77e5\u9053\u6b7b\u9501\u53d1\u751f\u5fc5\u987b\u8981\u6ee1\u8db3\u7684\u56db\u4e2a\u6761\u4ef6\uff0c\u6240\u4ee5\u53ea\u8981\u80fd\u6253\u7834\u4efb\u4f55\u5176\u4e2d\u4e00\u4e2a\u4ee5\u4e0a\u7684\u6761\u4ef6\u5373\u53ef\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5b8c\u3002<\/h1>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u662f\u7531\u6211\u7684\u7b14\u8bb0\u6574\u7406\u800c\u6210\uff0c\u603b\u7ed3\u4e86\u9762\u8bd5\u4e2d\u5e38\u89c1\u7684\u77e5\u8bc6\u70b9\uff0c\u540c\u65f6\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u4e5f\u662f\u5f88\u591a Java \u5f00\u53d1\u8005\u5bb9\u6613\u641e\u6df7\u5f04\u9519\u7684\u3002 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":741,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[24,48],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u57fa\u7840\/\u9762\u8bd5\u9898 - \u7801\u5148\u751f\u7684\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java\u57fa\u7840\/\u9762\u8bd5\u9898 - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\u662f\u7531\u6211\u7684\u7b14\u8bb0\u6574\u7406\u800c\u6210\uff0c\u603b\u7ed3\u4e86\u9762\u8bd5\u4e2d\u5e38\u89c1\u7684\u77e5\u8bc6\u70b9\uff0c\u540c\u65f6\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u4e5f\u662f\u5f88\u591a Java \u5f00\u53d1\u8005\u5bb9\u6613\u641e\u6df7\u5f04\u9519\u7684\u3002 ...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-02T14:30:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-25T15:24:38+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/java2-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"932\" \/>\n\t<meta property=\"og:image:height\" content=\"957\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"\u7801\u5148\u751f\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/mrcode2021\" \/>\n<meta name=\"twitter:site\" content=\"@mrcode2021\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u7801\u5148\u751f\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"Java\u57fa\u7840\/\u9762\u8bd5\u9898\",\"datePublished\":\"2023-02-02T14:30:47+00:00\",\"dateModified\":\"2023-03-25T15:24:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/\"},\"wordCount\":596,\"commentCount\":2,\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"keywords\":[\"Java\u9762\u7ecf\",\"\u9762\u8bd5\u9898\"],\"articleSection\":[\"JAVA\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/\",\"name\":\"Java\u57fa\u7840\/\u9762\u8bd5\u9898 - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2023-02-02T14:30:47+00:00\",\"dateModified\":\"2023-03-25T15:24:38+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u57fa\u7840\/\u9762\u8bd5\u9898\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/codermr.com\/#website\",\"url\":\"http:\/\/codermr.com\/\",\"name\":\"\u7801\u5148\u751f\u7684\u535a\u5ba2\",\"description\":\"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP\",\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/codermr.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\",\"name\":\"\u7801\u5148\u751f\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"contentUrl\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"width\":400,\"height\":400,\"caption\":\"\u7801\u5148\u751f\"},\"logo\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/codermr.com\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021\"],\"url\":\"http:\/\/codermr.com\/index.php\/author\/coderma\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java\u57fa\u7840\/\u9762\u8bd5\u9898 - \u7801\u5148\u751f\u7684\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/","og_locale":"zh_CN","og_type":"article","og_title":"Java\u57fa\u7840\/\u9762\u8bd5\u9898 - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"\u672c\u6587\u662f\u7531\u6211\u7684\u7b14\u8bb0\u6574\u7406\u800c\u6210\uff0c\u603b\u7ed3\u4e86\u9762\u8bd5\u4e2d\u5e38\u89c1\u7684\u77e5\u8bc6\u70b9\uff0c\u540c\u65f6\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u4e5f\u662f\u5f88\u591a Java \u5f00\u53d1\u8005\u5bb9\u6613\u641e\u6df7\u5f04\u9519\u7684\u3002 ...","og_url":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2023-02-02T14:30:47+00:00","article_modified_time":"2023-03-25T15:24:38+00:00","og_image":[{"width":932,"height":957,"url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/java2-1.jpg","type":"image\/jpeg"}],"author":"\u7801\u5148\u751f","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/mrcode2021","twitter_site":"@mrcode2021","twitter_misc":{"\u4f5c\u8005":"\u7801\u5148\u751f","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"Java\u57fa\u7840\/\u9762\u8bd5\u9898","datePublished":"2023-02-02T14:30:47+00:00","dateModified":"2023-03-25T15:24:38+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/"},"wordCount":596,"commentCount":2,"publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"keywords":["Java\u9762\u7ecf","\u9762\u8bd5\u9898"],"articleSection":["JAVA"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/","url":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/","name":"Java\u57fa\u7840\/\u9762\u8bd5\u9898 - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2023-02-02T14:30:47+00:00","dateModified":"2023-03-25T15:24:38+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2023\/02\/02\/java_mianjing1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"Java\u57fa\u7840\/\u9762\u8bd5\u9898"}]},{"@type":"WebSite","@id":"http:\/\/codermr.com\/#website","url":"http:\/\/codermr.com\/","name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","description":"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP","publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/codermr.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf","name":"\u7801\u5148\u751f","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/","url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","contentUrl":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","width":400,"height":400,"caption":"\u7801\u5148\u751f"},"logo":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/codermr.com","https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021"],"url":"http:\/\/codermr.com\/index.php\/author\/coderma\/"}]}},"_links":{"self":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/252"}],"collection":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":100,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":846,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions\/846"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media\/741"}],"wp:attachment":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}