{"id":306,"date":"2019-08-02T22:43:54","date_gmt":"2019-08-02T14:43:54","guid":{"rendered":"https:\/\/harson.co\/?p=306"},"modified":"2022-03-23T22:48:48","modified_gmt":"2022-03-23T14:48:48","slug":"java","status":"publish","type":"post","link":"https:\/\/harson.co\/index.php\/2019\/08\/02\/java\/","title":{"rendered":"Java"},"content":{"rendered":"<h1>\u5f00\u53d1\u73af\u5883<\/h1>\n<p>\u5c06JDK\u7684bin\u4e0b\u76ee\u5f55\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf\u4e0b\u5373\u53ef<\/p>\n<h1>\u57fa\u672c\u7ed3\u6784<\/h1>\n<pre class=\"pure-highlightjs\"><code class=\"\">public class Main\r\n{\r\n    public static void main(String[] args)\r\n    {\r\n        System.out.println(\"FuckWorld!!\");\r\n    }\r\n}<\/code><\/pre>\n<p>\u4e3b\u7c7b\u540d\u5fc5\u987b\u548c\u6587\u4ef6\u540d\u4e00\u81f4\uff0c\u4e0a\u5217\u793a\u4f8b\u4ee3\u7801\u610f\u5473\u7740\u6587\u4ef6\u540d\u4e3a <strong>Main.java.<\/strong><\/p>\n<p>\u4e3b\u51fd\u6570\u683c\u5f0f\u987b\u4e3a <strong>public static void main(String[] args)<\/strong><\/p>\n<h1>\u57fa\u672c\u8bed\u6cd5<\/h1>\n<p>\u8f83C\u8bed\u8a00\uff0c\u4ee5\u4e0b\u4e3a\u4e0d\u540c\u70b9<\/p>\n<p><strong>\u4e00\u4e9b\u53d8\u91cf\u8d4b\u503c<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">long a = 0L\r\nfloat a = 0.0f;\r\ndouble a = 0.0d;<\/code><\/pre>\n<p><strong>for\u5faa\u73af<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/\u9664C\u98ce\u683cfor\u5faa\u73af\u4ee5\u5916\uff0c\u8fd8\u53ef\u4ee5\u8fed\u4ee3\u5bf9\u8c61\u5faa\u73af\r\n\r\nint[] a = new int[10]\r\nfor (int i : a)\r\n{\r\n    system.out.print(i);\r\n}<\/code><\/pre>\n<p><strong>Collection\u63a5\u53e3\u6570\u7ec4<\/strong><\/p>\n<p><em>ArrayList <\/em>\u5e95\u5c42\u7531\u6570\u7ec4\u5b9e\u73b0\uff0c\u9002\u5408\u67e5\u8be2\u591a\u589e\u5220\u5c11\u7684\u60c5\u666f\u3002<\/p>\n<p><em>LinkedList <\/em>\u5e95\u5c42\u7531\u94fe\u8868\u5b9e\u73b0\uff0c\u9002\u5408\u67e5\u8be2\u5c11\u589e\u5220\u591a\u7684\u60c5\u666f\u3002<\/p>\n<h1>OOP<\/h1>\n<p><strong>\u4fee\u9970\u7b26<\/strong><\/p>\n<p><strong>default<\/strong>\u00a0(\u5373\u7f3a\u7701\uff0c\u4ec0\u4e48\u4e5f\u4e0d\u5199\uff09: \u5728\u540c\u4e00\u5305\u5185\u53ef\u89c1\uff0c\u4e0d\u4f7f\u7528\u4efb\u4f55\u4fee\u9970\u7b26\u3002\u4f7f\u7528\u5bf9\u8c61\uff1a\u7c7b\u3001\u63a5\u53e3\u3001\u53d8\u91cf\u3001\u65b9\u6cd5\u3002<\/p>\n<p><strong>private<\/strong>\u00a0: \u5728\u540c\u4e00\u7c7b\u5185\u53ef\u89c1\u3002\u4f7f\u7528\u5bf9\u8c61\uff1a\u53d8\u91cf\u3001\u65b9\u6cd5\u3002\u00a0<strong>\u6ce8\u610f\uff1a\u4e0d\u80fd\u4fee\u9970\u7c7b\uff08\u5916\u90e8\u7c7b\uff09<\/strong><\/p>\n<p><strong>public<\/strong>\u00a0: \u5bf9\u6240\u6709\u7c7b\u53ef\u89c1\u3002\u4f7f\u7528\u5bf9\u8c61\uff1a\u7c7b\u3001\u63a5\u53e3\u3001\u53d8\u91cf\u3001\u65b9\u6cd5<\/p>\n<p><strong>protected<\/strong>\u00a0: \u5bf9\u540c\u4e00\u5305\u5185\u7684\u7c7b\u548c\u6240\u6709\u5b50\u7c7b\u53ef\u89c1\u3002\u4f7f\u7528\u5bf9\u8c61\uff1a\u53d8\u91cf\u3001\u65b9\u6cd5\u3002\u00a0<strong>\u6ce8\u610f\uff1a\u4e0d\u80fd\u4fee\u9970\u7c7b\uff08\u5916\u90e8\u7c7b\uff09<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u591a\u6001<\/strong><\/p>\n<p>\uff08\u7b49\u53f7\uff09\u5de6\u7236\u53f3\u5b50\u4e3a\u591a\u6001<\/p>\n<p>\u53ea\u6709\u65b9\u6cd5\u80fd\u8986\u76d6\u91cd\u5199\uff0c\u53d8\u91cf\u4e0d\u80fd\u3002\u56e0\u800c\u5728\u591a\u6001\u4ee3\u7801\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u7236\u7c7b\u4e0b\u7684\u4e00\u4e2a\u5b50\u7c7b\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u5316\u5bf9\u8c61\u662f\u4e0d\u53ef\u8bbf\u95ee\u90a3\u4e2a\u5b50\u7c7b\u4e2d\u6240\u5b9a\u4e49\u7684\u53d8\u91cf\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/\u8bbf\u95ee\u6210\u5458\u53d8\u91cf\uff1a\r\n\r\n\/\/\u76f4\u63a5\u901a\u8fc7\u5bf9\u8c61\u540d\u79f0\u8bbf\u95ee\u6210\u5458\u53d8\u91cf\uff1a\u770b\u7b49\u53f7\u5de6\u8fb9\u662f\u8c01\uff0c\u4f18\u5148\u7528\u8c01\uff0c\u6ca1\u6709\u5219\u5411\u4e0a\u627e\u3002\r\n\r\nFu obj = new Zi();\r\n\/\/\u4f18\u5148\u7528Fu \u7c7b\u4e2d\u53d8\u91cf\uff0c\u6ca1\u6709\u5219\u5411\u4e0a\u627e<\/code><\/pre>\n<p>\u4e00\u4e2a\u5b9e\u73b0\u7c7b\uff08\u5c0f\u8303\u56f4\uff09\u53ef\u4ee5\u5411\u4e0a\u8f6c\u6362\u4e3a\u63a5\u53e3\u7c7b\u578b\uff0c\u53ef\u7c7b\u6bd4int\u81ea\u52a8\u8f6c\u6362\u4e3adouble\u3002<\/p>\n<p>\u56e0\u800c\uff0c\u4e00\u4e2a\u5b9e\u73b0\u7c7b\u662f\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\u7c7b\u578b\u4e3a\u63a5\u53e3\u7c7b\u7684\u65b9\u6cd5\u7684<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u91cd\u8f7d(overload)\u548c\u91cd\u5199(overide)<\/strong><\/p>\n<p>\u91cd\u8f7d\u5173\u6ce8\u65b9\u6cd5\u7684\u540d\u79f0\u548c\u53c2\u6570\u5217\u8868\u7684<strong>\u4e2a\u6570<\/strong>\u548c<strong>\u7c7b\u578b<\/strong>\uff0c\u4ed6\u4eec\u4e4b\u4e2d\u81f3\u5c11\u6709\u4e00\u9879\u5fc5\u987b<strong>\u4e0d<\/strong>\u4e00\u6837\u624d\u53ef\u79f0\u4e3a\u91cd\u8f7d\u3002<\/p>\n<p>\u91cd\u5199\u53ef\u4ee5\u53c2\u6570\u5217\u8868\u548c\u540d\u79f0\u5b8c\u5168\u4e00\u6837\uff0c\u4f46\u65b9\u6cd5\u5b9e\u73b0\u4e0d\u540c\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u4e00\u6837\uff0c\u56e0\u800c\u53ef\u4ee5\u6539\u53d8\u4ece\u7236\u7c7b\u7ee7\u627f\u8fc7\u6765\u7684\u4e00\u4e9b\u65b9\u6cd5<strong>\u5b9e\u73b0\u591a\u6001<\/strong>\u7684\u7279\u6027\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u533f\u540d\u5bf9\u8c61<\/strong><\/p>\n<p>\u533f\u540d\u5bf9\u8c61\u53ea\u53ef\u5b9e\u4f8b\u5316\u8c03\u7528\u4e00\u6b21\uff0c\u4e4b\u540e\u88abGC<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">new Person().name = \"Tim\";<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u6cdb\u578b<\/strong><\/p>\n<p>\u5f53\u4e00\u4e2a\u7c7b\u4e0d\u786e\u5b9a\u8981\u4f7f\u7528\u4ec0\u4e48\u7c7b\u578b\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b &lt;E e&gt; &lt;t&gt; \uff0c\u5728\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65f6\u5019\u518d\u786e\u5b9a\u5177\u4f53\u4f7f\u7528\u4ec0\u4e48\u6570\u636e\u7c7b\u578b<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">ArrayList &lt;String&gt; li = new ArrayList&lt;&gt;();<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a String\u7c7b\u578b\u7684 ArrayList<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">ArrayList li = new ArrayList();\r\n\/\/\u82e5\u4e0d\u5199\u6cdb\u578b\uff0c\u5219\u9ed8\u8ba4\u6570\u636e\u7c7b\u578b\u4e3aobject\uff0c\u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b<\/code><\/pre>\n<p>\u4f5c\u4e3a\u53c2\u6570\u5904\u7406\u6cdb\u578b\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6cdb\u578b\u901a\u914d\u7b26\u6765\u5904\u7406<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">public static void PrintArray(ArrayList&lt;?&gt; list)\r\n{\r\nIterator&lt;?&gt; it = list.iterator()\r\n....\r\n}<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5728\u4f20\u5165\u53c2\u6570\u65f6\uff0c\u5c31\u53ef\u81ea\u52a8\u8bc6\u522b\u5230\u5177\u4f53\u6cdb\u578b\u662f\u4ec0\u4e48\u3002<\/p>\n<p><strong>\u6cdb\u578b\u4e0a\u4e0b\u9650\u5b9a<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">public static void get(Collection&lt;? extends Number&gt; coll)\r\n\r\npublic static void get(Collection&lt;? super Number&gt; coll)<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u5e38\u7528\u5185\u7f6e\u65b9\u6cd5<\/strong><\/p>\n<p>Collections\u7c7b\u4e0b\u6570\u7ec4\u6392\u5e8f<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">ArraryList[] li  = new ArraryList[10];\r\n... \/\/\u8d4b\u503c\u64cd\u4f5c\r\n\r\nCollections.sort(li); \/\/\u9ed8\u8ba4\u5347\u5e8f\r\n\/\/\u82e5\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b\u6392\u5e8f\u65b9\u6cd5\uff0c\u9700\u8981\u91cd\u5199 compareTo\u65b9\u6cd5<\/code><\/pre>\n<p>Map\u63a5\u53e3\u96c6\u5408<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">Map&lt;String, String&gt; m = new HashMap&lt;&gt;();\r\n\/\/\u521b\u5efa\u952e\u503c\u5bf9\u96c6\u5408\r\nm.put(\"a\", \"Goods\");\r\n\/\/\u6dfb\u52a0\u952e\u503c\u5bf9\r\nString a = m.get(\"a\");\r\n\/\/\u5220\u9664\u952e\u503c\u5bf9\r\nm.remove(\"a\");<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h1>\u591a\u7ebf\u7a0b<\/h1>\n<p><strong>\u521b\u5efa\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u7b2c\u4e00\u79cd\u65b9\u5f0f:\u521b\u5efaThread\u7c7b\u7684\u5b50\u7c7b<\/strong><\/p>\n<p>java.lang.Thread\u7c7b:\u662f\u63cf\u8ff0\u7ebf\u7a0b\u7684\u7c7b,\u6211\u4eec\u60f3\u8981\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7a0b\u5e8f,\u5c31\u5fc5\u987b\u7ee7\u627fThread\u7c7b<\/p>\n<p>\u5b9e\u73b0\u6b65\u9aa4:<br \/>\n1.\u521b\u5efa\u4e00\u4e2aThread\u7c7b\u7684\u5b50\u7c7b<br \/>\n2.\u5728Thread\u7c7b\u7684\u5b50\u7c7b\u4e2d\u91cd\u5199Thread\u7c7b\u4e2d\u7684run\u65b9\u6cd5,\u8bbe\u7f6e\u7ebf\u7a0b\u4efb\u52a1(\u5f00\u542f\u7ebf\u7a0b\u8981\u505a\u4ec0\u4e48?)<br \/>\n3.\u521b\u5efaThread\u7c7b\u7684\u5b50\u7c7b\u5bf9\u8c61<br \/>\n4.\u8c03\u7528Thread\u7c7b\u4e2d\u7684\u65b9\u6cd5start\u65b9\u6cd5,\u5f00\u542f\u65b0\u7684\u7ebf\u7a0b,\u6267\u884crun\u65b9\u6cd5<br \/>\nvoid start() \u4f7f\u8be5\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\uff1bJava \u865a\u62df\u673a\u8c03\u7528\u8be5\u7ebf\u7a0b\u7684 run \u65b9\u6cd5\u3002<br \/>\n\u7ed3\u679c\u662f\u4e24\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u5730\u8fd0\u884c\uff1b\u5f53\u524d\u7ebf\u7a0b\uff08main\u7ebf\u7a0b\uff09\u548c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\uff08\u521b\u5efa\u7684\u65b0\u7ebf\u7a0b,\u6267\u884c\u5176 run \u65b9\u6cd5\uff09\u3002<br \/>\n\u591a\u6b21\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u662f\u975e\u6cd5\u7684\u3002\u7279\u522b\u662f\u5f53\u7ebf\u7a0b\u5df2\u7ecf\u7ed3\u675f\u6267\u884c\u540e\uff0c\u4e0d\u80fd\u518d\u91cd\u65b0\u542f\u52a8\u3002<br \/>\njava\u7a0b\u5e8f\u5c5e\u4e8e\u62a2\u5360\u5f0f\u8c03\u5ea6,\u90a3\u4e2a\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u9ad8,\u90a3\u4e2a\u7ebf\u7a0b\u4f18\u5148\u6267\u884c;\u540c\u4e00\u4e2a\u4f18\u5148\u7ea7,\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u6267\u884c<\/p>\n<p><strong>\u83b7\u53d6\u7ebf\u7a0b\u540d\/\u91cd\u547d\u540d\u7ebf\u7a0b<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u540d\r\nThread.currentThread().getName()\r\n\/\/\u4fee\u6539\u7ebf\u7a0b\u540d\r\nmt.setname(\"NewName\");<\/code><\/pre>\n<p><strong>sleep\u65b9\u6cd5<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">Thread.sleep(1000);\r\n\/\/\u505c\u4e00\u79d2<\/code><\/pre>\n<p><strong>\u521b\u5efa\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u7b2c\u4e8c\u79cd\u65b9\u5f0f:\u5b9e\u73b0Runnable\u63a5\u53e3<\/strong><br \/>\njava. lang. Runnable<br \/>\nRunnable\u63a5\u53e3\u5e94\u8be5\u7531\u90a3\u4e9b\u6253\u7b97\u901a\u8fc7\u67d0\u4e00\u7ebf\u7a0b\u6267\u884c\u5176\u5b9e\u4f8b\u7684\u7c7b\u6765\u5b9e\u73b0\u3002\u7c7b\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e00\u4e2a\u79f0\u4e3arun \u7684\u65e0\u53c2\u6570\u65b9\u6cd5\u3002<br \/>\njava. lang. Thread\u7c7b\u7684\u6784\u9020\u65b9\u6cd5<br \/>\nThread(Runnable target)\u5206\u914d\u65b0\u7684Thread \u5bf9\u8c61\u3002<br \/>\nThread(Runnable target, String name)\u5206\u914d\u65b0\u7684Thread \u5bf9\u8c61\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u6b65\u9aa4:<\/strong><br \/>\n1.\u521b\u5efa\u4e00\u4e2aRunnable\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b<br \/>\n2.\u5728\u5b9e\u73b0\u7c7b\u4e2d\u91cd\u5199Runnabl e\u63a5\u53e3\u7684run\u65b9\u6cd5,\u8bbe\u7f6e\u7ebf\u7a0b\u4efb\u52a1<br \/>\n3.\u521b\u5efa- -\u4e2aRunnable\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\u5bf9\u8c61<br \/>\n4.\u521b\u5efaThread\u7c7b\u5bf9\u8c61,\u6784\u9020\u65b9\u6cd5\u4e2d\u4f20\u9012Runnable\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\u5bf9\u8c61<br \/>\n5.\u8c03\u7528Thread\u7c7b\u4e2d\u7684start\u65b9\u6cd5,\u5f00\u542f\u65b0\u7684\u7ebf\u7a0b\u6267\u884crun\u65b9\u6cd5<br \/>\n\u5b9e\u73b0Runnable\u63a5\u53e3\u521b\u5efa\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u597d\u5904:<br \/>\n1.<strong>\u907f\u514d\u4e86\u5355\u7ee7\u627f\u7684\u5c40\u9650\u6027<\/strong><br \/>\n\u4e00\u4e2a\u7c7b\u53ea\u80fd\u7ee7\u627f\u4e00\u4e2a\u7c7b(\u4e00\u4e2a\u4eba\u53ea\u80fd\u6709\u4e00\u4e2a\u4eb2\u7239) ,\u7c7b\u7ee7\u627f\u4e86Thread\u7c7b\u5c31\u4e0d\u80fd\u7ee7\u627f\u5176\u4ed6\u7684\u7c7b<br \/>\n\u5b9e\u73b0\u4e86Runnable\u63a5\u53e3\uff0c\u8fd8\u53ef\u4ee5\u7ee7\u627f\u5176\u4ed6\u7684\u7c7b,\u5b9e\u73b0\u5176\u4ed6\u7684\u63a5\u53e3<br \/>\n2.<strong>\u589e\u5f3a\u4e86\u7a0b\u5e8f\u7684\u6269\u5c55\u6027,\u964d\u4f4e\u4e86\u7a0b\u5e8f\u7684\u8026\u5408\u6027(\u89e3\u8026)<\/strong><br \/>\n\u5b9e\u73b0Runnable\u63a5\u53e3\u7684\u65b9\u5f0f\uff0c\u628a\u8bbe\u7f6e\u7ebf\u7a0b\u4efb\u52a1\u548c\u5f00\u542f\u65b0\u7ebf\u7a0b\u8fdb\u884c\u4e86\u5206\u79bb(\u89e3\u8026)<br \/>\n\u5b9e\u73b0\u7c7b\u4e2d,\u91cd\u5199\u4e86run\u65b9\u6cd5:\u7528\u6765\u8bbe\u7f6e\u7ebf\u7a0b\u4efb\u52a1<br \/>\n\u521b\u5efaThread\u7c7b\u5bf9\u8c61,\u8c03\u7528start\u65b9\u6cd5:\u7528\u6765\u5f00\u542f\u65b0\u7ebf\u7a0b<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u4ee3\u7801\u540c\u6b65<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/\u521b\u5efa\u4e00\u4e2a\u9501\u5bf9\u8c61\r\nObject obj = new Object();\r\n\/\/\u540c\u6b65\u4ee3\u7801\u5757\r\n\r\nsynchronized(obj)\r\n{\r\n\r\n...\r\n}\r\n\r\n\/\/\u6216\u8005\u4f7f\u7528synchronized \u4fee\u9970\u7b26\u65b9\u6cd5<\/code><\/pre>\n<p><strong>\u540c\u6b65\u6280\u672f\u7684\u539f\u7406:<\/strong><br \/>\n\u4f7f\u7528\u4e86\u4e00\u4e2a\u9501\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u9501\u5bf9\u8c61\u53eb\u540c\u6b65\u9501\u4e5f\u53eb\u5bf9\u8c61\u9501\u4e5f\u53eb\u5bf9\u8c61\u76d1\u89c6\u5668<br \/>\n3\u4e2a\u7ebf\u7a0b\u8d77\u62a2\u593acpu\u7684\u6267\u884c\u6743\u8c01\u62a2\u5230\u4e86\u8c01\u6267\u884crun\u65b9\u6cd5\u8fdb\u884c\u5356\u7968<br \/>\nt0\u62a2\u5230\u4e86cpu\u7684\u6267\u884c\u6743\u6267\u884crun\u65b9\u6cd5\u9047\u5230synchronized\u4ee3\u7801\u5757<br \/>\n\u8fd9\u65f6t\u4f1a\u68c0\u67e5synchronized\u4ee3\u7801\u5757\u662f\u5426\u6709\u9501\u5bf9\u8c61<br \/>\n\u53d1\u73b0\u6709,\u5c31\u4f1a\u83b7\u53d6\u5230\u9501\u5bf9\u8c61\u8fdb\u5165\u5230\u540c\u6b65\u4e2d\u6267\u884c<br \/>\nt1\u62a2\u5230\u4e86cpu\u7684\u6267\u884c\u6743\u6267\u884crun\u65b9\u6cd5\u9047\u5230synchronized\u4ee3\u7801\u5757<br \/>\n\u8fd9\u65f6t1\u4f1a\u68c0\u67e5synchronized\u4ee3\u7801\u5757\u662f\u5426\u6709\u9501\u5bf9\u8c61<br \/>\n\u53d1\u73b0\u6ca1\u6709,t1\u6551\u62a4\u8fdb\u5165\u5230\u963b\u585e\u72b6\u6001\u4f1a\u4e00\u76f4\u7b49\u5f85t0\u7ebf\u7a0b\u5f52\u8fd8\u9501\u5bf9\u8c61<br \/>\n\u4e00\u76f4\u5230t0\u7ebf\u7a0b\u6267\u884c\u5b8c\u540c\u6b65\u4e2d\u7684\u4ee3\u7801,\u4f1a\u628a\u9501\u5bf9\u8c61\u5f52\u8fd8\u7ed9\u540c\u6b65\u4ee3\u7801\u5757<br \/>\nt1\u624d\u80fd\u83b7\u53d6\u5230\u9501\u5bf9\u8c61\u8fdb\u5165\u5230\u540c\u6b65\u4e2d\u6267\u884c<\/p>\n<p>\u603b\u7ed3:\u540c\u6b65\u4e2d\u7684\u7ebf\u7a0b\u6ca1\u6709\u6267\u884c\u5b8c\u6bd5\u4e0d\u4f1a\u91ca\u653e\u9501\u540c\u6b65\u5916\u7684\u7ebf\u7a0b\u6ca1\u6709\u9501\u8fdb\u4e0d\u53bb\u540c\u6b65<\/p>\n<p><strong>Lock\u9501\u63a5\u53e3\u4e5f\u53ef\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u7ebf\u7a0b\u6c60<\/strong><\/p>\n<p>\u7ebf\u7a0b\u6c60:IDK1.5\u4e4b\u540e\u63d0\u4f9b\u7684<br \/>\njava. util. concurrent . Executors :\u7ebf\u7a0b\u6c60\u7684\u5de5\u5382\u7c7b\uff0c\u7528\u6765\u751f\u6210\u7ebf\u7a0b\u6c60<br \/>\nExecutors\u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5:<br \/>\nstatic ExecutorService newFixedThreadPool(int nThreads) \u521b\u5efa-\u4e00\u4e2a\u53ef\u91cd\u7528\u56fa\u5b9a\u7ebf\u7a0b\u6570\u7684\u7ebf\u7a0b\u6c60<br \/>\n\u53c2\u6570:<br \/>\nint nThreads :\u521b\u5efa\u7ebf\u7a0b\u6c60\u4e2d\u5305\u542b\u7684\u7ebf\u7a0b\u6570\u91cf<br \/>\n\u8fd4\u56de\u503c:<br \/>\nExecutorService\u63a5\u53e3,\u8fd4\u56de\u7684\u662fExecutorService\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\u5bf9\u8c61,\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Executorservice\u63a5\u53e3\u63a5\u6536(\u9762\u5411\u63a5\u53e3\u7f16\u7a0b)<br \/>\njava. util. concurrent. Executorservice:\u7ebf\u7a0b\u6c60\u63a5\u53e3<br \/>\n\u7528\u6765\u4ece\u7ebf\u7a0b\u6c60\u4e2d\u83b7\u53d6\u7ebf\u7a0b,\u8c03\u7528start\u65b9\u6cd5\uff0c\u6267\u884c\u7ebf\u7a0b\u4efb\u52a1<br \/>\nsubmit(Runnable task) \u63d0\u4ea4\u4e00\u4e2aRunnable \u4efb\u52a1\u7528\u4e8e\u6267\u884c<br \/>\n\u5173\u95ed\u9500\u6bc1\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5<br \/>\nvoid shutdown()<br \/>\n\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528\u6b65\u9aa4:<br \/>\n1.\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u5de5\u5382\u7c7bExecutors\u91cc\u8fb9\u63d0\u4f9b\u7684\u9759\u6001\u65b9\u6cd5newF ixedThreadPool\u751f\u4ea7-\u4e00\u4e2a\u6307\u5b9a\u7ebf\u7a0b\u6570\u91cf\u7684\u7ebf\u7a0b\u6c60<br \/>\n\u7389\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5b9e\u73b0Runnable\u63a5\u53e3\uff0c\u91cd\u5199run\u65b9\u6cd5,\u8bbe\u5668\u7ebf\u7a0b\u4efb\u52a1<br \/>\n3.\u8c03\u7528ExecutorService\u4e2d\u7684\u65b9\u6cd5submit,\u4f20\u9012\u7ebf\u7a0b\u4efb\u52a1(\u5b9e\u73b0\u7c7b),\u5f00\u542f\u7ebf\u7a0b,\u6267\u884crun\u65b9\u6cd5<br \/>\n4.\u8c03\u7528ExecutorService\u4e2d\u7684\u65b9\u6cd5shutdown\u9500\u6bc1\u7ebf\u7a0b\u6c60(\u4e0d\u5efa\u8bae\u6267\u884c)<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\u73af\u5883 \u5c06JDK\u7684bin\u4e0b\u76ee\u5f55\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf\u4e0b\u5373\u53ef \u57fa\u672c\u7ed3\u6784 public class Main { pub [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/comments?post=306"}],"version-history":[{"count":11,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/306\/revisions\/577"}],"wp:attachment":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}