{"id":79,"date":"2023-02-13T10:43:57","date_gmt":"2023-02-13T02:43:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=79"},"modified":"2023-02-13T10:45:41","modified_gmt":"2023-02-13T02:45:41","slug":"java-multi-threag-programming-synchronized","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/13\/java-multi-threag-programming-synchronized\/","title":{"rendered":"Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e8c\uff08synchronized\u540c\u6b65\u65b9\u6cd5\u548csynchronized\u540c\u6b65\u4ee3\u7801\u5757\uff09"},"content":{"rendered":"<h2>synchronized\u540c\u6b65\u65b9\u6cd5<\/h2>\n<h3>\u65b9\u6cd5\u5185\u7684\u53d8\u91cf\u4e3a\u7ebf\u7a0b\u5b89\u5168<\/h3>\n<p>\u201c\u975e\u7ebf\u7a0b\u5b89\u5168\u201d\u95ee\u9898\u5b58\u5728\u4e0e\u5b9e\u4f8b\u53d8\u91cf\u4e2d\uff0c\u5982\u679c\u662f\u65b9\u6cd5\u5185\u90e8\u7684\u79c1\u6709\u53d8\u91cf\uff0c\u5219\u4e0d\u5b58\u5728\u201c\u975e\u7ebf\u7a0b\u5b89\u5168\u201d\u95ee\u9898\uff0c\u4e5f\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<pre><code class=\"language-java\">public class MyThread extends Thread {\n    @Override\n    public void run() {\n        int count = 5;\n        System.out.println(&quot;\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1&quot;);\n        while (count &gt; 0) {\n            count--;\n            System.out.println(&quot;\u7531\uff1a&quot; + Thread.currentThread().getName() + &quot;\u8ba1\u7b97\uff0ccount=&quot; + count);\n        }\n    }\n\n    public static void main(String[] args) {\n        MyThread myThread = new MyThread();\n        Thread a = new Thread(myThread, &quot;A&quot;);\n        Thread b = new Thread(myThread, &quot;B&quot;);\n        Thread c = new Thread(myThread, &quot;C&quot;);\n        a.start();\n        b.start();\n        c.start();\n        System.out.println(&quot;\u8fd0\u884c\u7ed3\u675f\uff01&quot;);\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=4\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=3\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=2\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=1\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=0\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=4\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=3\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=2\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=1\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=0\n\u8fd0\u884c\u7ed3\u675f\uff01\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aC\u8ba1\u7b97\uff0ccount=4\n\u7531\uff1aC\u8ba1\u7b97\uff0ccount=3\n\u7531\uff1aC\u8ba1\u7b97\uff0ccount=2\n\u7531\uff1aC\u8ba1\u7b97\uff0ccount=1\n\u7531\uff1aC\u8ba1\u7b97\uff0ccount=0<\/code><\/pre>\n<p>\u5c3d\u7ba1\u6253\u5370\u7684\u5185\u5bb9\u4e0d\u89c4\u5219\uff0c\u4f46\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<h3>\u5b9e\u4f8b\u53d8\u91cf\u975e\u7ebf\u7a0b\u5b89\u5168<\/h3>\n<p>\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u5171\u540c\u8bbf\u95ee\u540c\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u7684\u5b9e\u4f8b\u53d8\u91cf\uff0c\u5219\u53ef\u80fd\u51fa\u73b0\u201c\u975e\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u201d\u3002<\/p>\n<p>\u66f4\u6539\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u5c06\u65b9\u6cd5\u5185\u79c1\u6709\u53d8\u91cf\u66f4\u6539\u4e3a\uff0c\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u7684\u79c1\u6709\u53d8\u91cf\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyThread extends Thread {\n\n    private int count = 5;\n\n    @Override\n    public void run() {\n        System.out.println(&quot;\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1&quot;);\n        count--;\n        System.out.println(&quot;\u7531\uff1a&quot; + Thread.currentThread().getName() + &quot;\u8ba1\u7b97\uff0ccount=&quot; + count)\uff1b\n    }\n\n    public static void main(String[] args) {\n        MyThread myThread = new MyThread();\n        Thread a = new Thread(myThread, &quot;A&quot;);\n        Thread b = new Thread(myThread, &quot;B&quot;);\n        Thread c = new Thread(myThread, &quot;C&quot;);\n        a.start();\n        b.start();\n        c.start();\n        System.out.println(&quot;\u8fd0\u884c\u7ed3\u675f\uff01&quot;);\n    }\n}<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=3\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=3\n\u8fd0\u884c\u7ed3\u675f\uff01\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aC\u8ba1\u7b97\uff0ccount=2<\/code><\/pre>\n<p>\u7ed3\u679c\u51fa\u73b0\u4e86\u975e\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u5047\u5982\u8fd9\u4e2a\u662f\u62a2\u7968\uff0c\u5219\u4f1a\u51fa\u73b0\u5927\u95ee\u9898\u4e86\uff0c\u89e3\u51b3\u6b64\u95ee\u9898\u53ea\u9700\u8981\u5728\u65b9\u6cd5(run\u65b9\u6cd5)\u524d\u52a0\u4e0a<code>synchronized<\/code>\u5173\u952e\u5b57\u5373\u53ef\uff0c\u66f4\u6539\u540e\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=4\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=3\n\u8fd0\u884c\u7ed3\u675f\uff01\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aC\u8ba1\u7b97\uff0ccount=2<\/code><\/pre>\n<p>\u6ca1\u6709\u518d\u51fa\u6570\u636e\u9519\u8bef\u7684\u60c5\u51b5\uff0c\u8fd9\u6837\u8f6c\u6362\u4e3a\u540c\u6b65\u65b9\u6cd5\u5c31\u89e3\u51b3\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<h3>\u591a\u4e2a\u5bf9\u8c61\u591a\u4e2a\u9501<\/h3>\n<p>synchronized\u53d6\u5f97\u7684\u9501\u90fd\u662f\u5bf9\u8c61\u9501\uff0c\u800c\u4e0d\u662f\u628a\u4e00\u6bb5\u4ee3\u7801\u6216\u65b9\u6cd5\uff08\u51fd\u6570\uff09\u5f53\u4f5c\u9501\uff0c\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u6b65\u65b9\u6cd5\u65f6\uff0c\u54ea\u4e2a\u7ebf\u7a0b\u5148\u6267\u884c\u540c\u6b65\u65b9\u6cd5\uff0c\u54ea\u4e2a\u7ebf\u7a0b\u5c31\u6301\u6709\u8be5\u65b9\u6cd5\u6240\u5c5e\u5bf9\u8c61\u7684\u9501lock\uff0c\u5176\u4ed6\u7ebf\u7a0b\u53ea\u80fd\u7b49\u5f85\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u8bbf\u95ee\u5b8c\u6bd5\uff0c\u5728\u8fdb\u884c\u4e89\u593a\u9501\u3002<\/p>\n<h3>synchronized\u65b9\u6cd5\u4e0e\u9501\u5bf9\u8c61<\/h3>\n<p>\u8c03\u7528\u5173\u952e\u5b57<code>synchronized<\/code>\u58f0\u660e\u7684\u65b9\u6cd5\u4e00\u5b9a\u6392\u961f\u8fd0\u884c\u7684\uff0c\u53e6\u5916\u9700\u8981\u7262\u8bb0\u201c\u5171\u4eab\u201d\u4e24\u4e2a\u5b57\uff0c\u53ea\u6709\u5171\u4eab\u8d44\u6e90\u7684\u8bfb\u5199\u8bbf\u95ee\u624d\u9700\u8981\u540c\u6b65\u5316\uff0c\u5982\u679c\u4e0d\u662f\u5171\u4eab\u7684\u8d44\u6e90\uff0c\u5c31\u6ca1\u6709\u4f7f\u7528\u540c\u6b65\u7684\u5fc5\u8981\u3002<\/p>\n<p>\u7ebf\u7a0bA\u548c\u7ebf\u7a0bB\uff0c\u5982\u679cA\u73b0\u6301\u6709\u4e86\u5bf9\u8c61\u7684<code>Lock<\/code>\u9501\u8bbf\u95ee<code>synchronized<\/code>\u65b9\u6cd5\uff0c\u90a3\u4e48B\u65e0\u6cd5\u8bbf\u95ee\u88ab\u6b64\u540c\u6b65\u65b9\u6cd5\uff0c\u82e5\u4e00\u5b9a\u8981\u8bbf\u95ee\u6b64\u540c\u6b65\u65b9\u6cd5\u5219\u9700\u7b49\u5f85\uff0c\u4f46\u662fB\u53ef\u4ee5\u5f02\u6b65\u8c03\u7528\u975e<code>synchronized<\/code>\u7c7b\u578b\u7684\u65b9\u6cd5\u3002<\/p>\n<h3>\u810f\u8bfb<\/h3>\n<p>\u4e3a\u4e86\u907f\u514d\u6570\u636e\u51fa\u73b0\u4ea4\u53c9\u3001\u51fa\u9519\uff0c\u4f7f\u7528<code>synchronized<\/code>\u5173\u952e\u5b57\u6765\u8fdb\u884c\u540c\u6b65\uff0c\u867d\u7136\u5728\u8d4b\u503c\u7684\u65f6\u5019\u8fdb\u884c\u540c\u6b65\uff0c\u4f46\u5728\u53d6\u503c\u7684\u65f6\u5019\u6709\u53ef\u80fd\u51fa\u73b0\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u610f\u5916\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u662f\u810f\u8bfb\uff0c\u53d1\u751f\u810f\u8bfb\u7684\u60c5\u51b5\u662f\u5728\u8bfb\u53d6\u5b9e\u4f8b\u53d8\u91cf\u65f6\uff0c\u6b64\u503c\u5df2\u7ecf\u88ab\u5176\u4ed6\u7ebf\u7a0b\u66f4\u6539\u8fc7\u4e86\u3002\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u5728\u53d6\u503c\u7684\u65b9\u6cd5\u4e0a\u52a0\u4e0a<code>synchronized<\/code>\u5173\u952e\u5b57\u4f7f\u5176\u6210\u4e3a\u540c\u6b65\u65b9\u6cd5\uff0c\u8fd9\u6837\u5c31\u5fc5\u987b\u7b49\u5f85\u5176\u4ed6\u4e00\u7cfb\u5217\u7684\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5728\u8fdb\u884c\u8bfb\u53d6\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u6765\u810f\u8bfb\u3002\u603b\u7ed3\uff1a<\/p>\n<ul>\n<li>\u5f53A\u7ebf\u7a0b\u8c03\u7528anyObject\u5bf9\u8c61\u52a0\u5165<code>synchronized<\/code>\u5173\u952e\u5b57\u7684X\u65b9\u6cd5\u65f6\uff0cA\u7ebf\u7a0b\u5c31\u83b7\u5f97\u4e86X\u65b9\u6cd5\u9501\uff0c\u66f4\u51c6\u5907\u5730\u8bb2\uff0c\u662f\u83b7\u5f97\u4e86\u5bf9\u8c61\u7684\u9501\uff0c\u6240\u4ee5\u5176\u4ed6\u7ebf\u7a0b\u5fc5\u987b\u7b49A\u7ebf\u7a0b\u6267\u884c\u5b8c\u624d\u53ef\u4ee5\u8c03\u7528X\u65b9\u6cd5\uff0c\u4f46B\u7ebf\u7a0b\u53ef\u4ee5\u968f\u610f\u8c03\u7528\u5176\u4ed6\u7684\u975e<code>synchronized<\/code>\u540c\u6b65\u65b9\u6cd5\u3002<\/li>\n<li>\u5f53A\u7ebf\u7a0b\u8c03\u7528anyObject\u5bf9\u8c61\u52a0\u5165<code>synchronized<\/code>\u5173\u952e\u5b57\u7684X\u65b9\u6cd5\u65f6\uff0cA\u7ebf\u7a0b\u5c31\u83b7\u5f97\u4e86X\u65b9\u6cd5\u6240\u5728\u5bf9\u8c61\u7684\u9501\uff0c\u6240\u4ee5\u5176\u4ed6\u7ebf\u7a0b\u5fc5\u987b\u7b49A\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u624d\u53ef\u4ee5\u8c03\u7528X\u65b9\u6cd5\uff0c\u800cB\u7ebf\u7a0b\u5982\u679c\u8c03\u7528\u58f0\u660e\u4e86<code>synchronized<\/code>\u5173\u952e\u5b57\u7684\u975eX\u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u7b49A\u7ebf\u7a0b\u5c06X\u65b9\u6cd5\u6267\u884c\u5b8c\uff0c\u4e5f\u5c31\u662f\u91ca\u653e\u5bf9\u8c61\u9501\u540e\u624d\u53ef\u4ee5\u8c03\u7528\u3002\u8fd9\u65f6A\u7ebf\u7a0b\u5df2\u7ecf\u6267\u884c\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u4efb\u52a1\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e9b\u53d8\u91cf\u7684\u8d4b\u503c\u5df2\u7ecf\u5b8c\u6210\u4e0d\u5b58\u5728\u810f\u8bfb\u7684\u73af\u5883\u4e86\u3002<\/li>\n<\/ul>\n<h3>synchronized\u9501\u91cd\u5165<\/h3>\n<p>\u5173\u952e\u5b57<code>synchronized<\/code>\u62e5\u6709\u9501\u91cd\u5165\u7684\u529f\u80fd\uff0c\u4e5f\u5c31\u662f\u5728\u4f7f\u7528<code>synchronized<\/code>\u65f6\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\u9501\u540e\uff0c\u518d\u6b21\u8bf7\u6c42\u6b64\u5bf9\u8c61\u9501\u65f6\u662f\u53ef\u4ee5\u518d\u6b21\u5f97\u5230\u8be5\u5bf9\u8c61\u7684\u9501\u7684\u3002\u4e00\u4e2a<code>synchronized<\/code>\u65b9\u6cd5\/\u5757\u7684\u5185\u90e8\u8c03\u7528\u672c\u7c7b\u7684\u5176\u4ed6<code>synchronized<\/code>\u65b9\u6cd5\/\u5757\u65f6\uff0c\u662f\u6c38\u8fdc\u53ef\u4ee5\u5f97\u5230\u9501\u7684\u3002<\/p>\n<h3>\u51fa\u73b0\u5f02\u5e38\u9501\u81ea\u52a8\u91ca\u653e<\/h3>\n<p>\u7ebf\u7a0ba\u6301\u7528\u5bf9\u8c61\u7684\u9501\uff0c\u6b64\u65f6\u53d1\u751f\u5f02\u5e38\u5219a\u4f1a\u662f\u91ca\u653e\u9501\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7ebf\u7a0b\u80fd\u591f\u6210\u529f\u62ff\u5230\u9501\u6765\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n<h3>\u540c\u6b65\u4e0d\u5177\u6709\u7ee7\u627f\u6027<\/h3>\n<p>\u540c\u6b65\u4e0d\u80fd\u7ee7\u627f\uff0c\u5982\u679c\u5b50\u7c7b\u91cd\u5199\u4e86\u7236\u7c7b\u7684\u540c\u6b65\u65b9\u6cd5\uff0c\u5219\u9700\u8981\u5728\u5b50\u7c7b\u7684\u540c\u6b65\u65b9\u6cd5\u4e2d\u6dfb\u52a0<code>synchronized<\/code>\u5173\u952e\u5b57\uff0c\u5426\u5219\u65e0\u6cd5\u540c\u6b65\u3002<\/p>\n<h2>\u540c\u6b65\u8bed\u53e5\u5757<\/h2>\n<p>\u7528\u5173\u952e\u5b57<code>synchronized<\/code>\u58f0\u660e\u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u662f\u6709\u5f0a\u7aef\u7684\uff0c\u6bd4\u5982A\u7ebf\u7a0b\u8c03\u7528\u540c\u6b65\u65b9\u6cd5\u6267\u884c\u4e86\u4e00\u4e2a\u957f\u65f6\u95f4\u7684\u4efb\u52a1\uff0c\u90a3\u4e48B\u7ebf\u7a0b\u5219\u5fc5\u987b\u7b49\u5f85\u8f83\u957f\u65f6\u95f4\u3002\u5728\u8fd9\u6837\u7684\u60c5\u51b5\u4e0b\u5c31\u8981\u8003\u8651\u4f7f\u7528<code>synchronized<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u6765\u89e3\u51b3\u3002<\/p>\n<h3>synchronized\u540c\u6b65\u4ee3\u7801\u5757\u7684\u4f7f\u7528<\/h3>\n<pre><code class=\"language-java\">public class MyService {\n\n    public void serviceMethod() {\n        try {\n            synchronized (this) {\/\/\u4f7f\u7528\u540c\u6b65\u4ee3\u7801\u5757\n                System.out.println(&quot;begin time = &quot; + new Date());\n                Thread.sleep(2000);\n                System.out.println(&quot;end   time = &quot; + new Date());\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    static class ThreadA extends Thread {\n        private MyService myService;\n\n        ThreadA(MyService myService) {\n            this.myService = myService;\n        }\n\n        @Override\n        public void run() {\n            super.run();\n            myService.serviceMethod();\n        }\n    }\n\n    static class ThreadB extends Thread {\n        private MyService myService;\n\n        ThreadB(MyService myService) {\n            this.myService = myService;\n        }\n\n        @Override\n        public void run() {\n            super.run();\n            myService.serviceMethod();\n        }\n    }\n\n    public static void main(String[] args) {\n        MyService myService = new MyService();\n\n        MyService.ThreadA threadA = new MyService.ThreadA(myService);\n        threadA.setName(&quot;a&quot;);\n        threadA.start();\n\n        MyService.ThreadB threadB = new MyService.ThreadB(myService);\n        threadB.setName(&quot;b&quot;);\n        threadB.start();\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>begin time = Tue Feb 19 13:51:49 CST 2019\nend   time = Tue Feb 19 13:51:51 CST 2019\nbegin time = Tue Feb 19 13:51:51 CST 2019\nend   time = Tue Feb 19 13:51:53 CST 2019<\/code><\/pre>\n<p>\u7ed3\u679c\u6765\u770b\u5e76\u672a\u51fa\u73b0\u975e\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u4f46\u662f\u6267\u884c\u6548\u7387\u786e\u5b9e\u5f88\u4f4e\u5e76\u6ca1\u6709\u63d0\u9ad8\uff0c\u6267\u884c\u7684\u6548\u679c\u8fd8\u662f\u540c\u6b65\u8fd0\u884c\u7684\u3002<\/p>\n<h3>\u7528\u540c\u6b65\u4ee3\u7801\u5757\u89e3\u51b3\u540c\u6b65\u65b9\u6cd5\u7684\u5f0a\u7aef\uff08\u4e00\u534a\u540c\u6b65\uff0c\u4e00\u534a\u5f02\u6b65\uff09<\/h3>\n<p>\u4fee\u6539\u4e0a\u9762MyService\u4e2dserviceMethod()\u65b9\u6cd5\uff0c\u5176\u4ed6\u4e0d\u53d8\uff1a<\/p>\n<pre><code class=\"language-java\">public void serviceMethod() {\n    try {\n        for (int i = 0; i &lt; 5; i++) {\n            System.out.println(&quot;nosynchronized threadName=&quot; + Thread.currentThread().getName() + &quot;  i=&quot; + (i + 1));\n            Thread.sleep(1000);\n        }\n        synchronized (this) {\n            for (int i = 0; i &lt; 5; i++) {\n                System.out.println(&quot;synchronized threadName=&quot; + Thread.currentThread().getName() + &quot;  i=&quot; + (i + 1));\n                Thread.sleep(1000);\n            }\n        }\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>nosynchronized threadName=a  i=1\nnosynchronized threadName=b  i=1\nnosynchronized threadName=a  i=2\nnosynchronized threadName=b  i=2\nnosynchronized threadName=b  i=3\nnosynchronized threadName=a  i=3\nnosynchronized threadName=b  i=4\nnosynchronized threadName=a  i=4\nnosynchronized threadName=b  i=5\nnosynchronized threadName=a  i=5\nsynchronized threadName=b  i=1\nsynchronized threadName=b  i=2\nsynchronized threadName=b  i=3\nsynchronized threadName=b  i=4\nsynchronized threadName=b  i=5\nsynchronized threadName=a  i=1\nsynchronized threadName=a  i=2\nsynchronized threadName=a  i=3\nsynchronized threadName=a  i=4\nsynchronized threadName=a  i=5<\/code><\/pre>\n<p>\u7531\u7ed3\u679c\u53ef\u77e5\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95eeobject\u7684\u4e00\u4e2a<code>synchronized<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u65f6\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u8be5object\u5bf9\u8c61\u4e2d\u7684\u975e<code>synchronized(this)<\/code>\u540c\u6b65\u4ee3\u7801\u5757\uff0c\u6709\u6548\u7684\u63d0\u9ad8\u4e86\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<h3>synchronized\u4ee3\u7801\u5757\u95f4\u7684\u540c\u6b65\u6027<\/h3>\n<ul>\n<li>\n<p>\u5728\u4f7f\u7528<code>synchronized(this)<\/code>\u4ee3\u7801\u5757\u65f6\u9700\u8981\u6ce8\u610f\u7684\u65f6\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95eeobject\u7684\u4e00\u4e2a<code>synchronized(this)<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u65f6\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5bf9\u540c\u4e00\u4e2aobject\u4e2d\u6240\u6709\u5176\u4ed6<code>synchronized(this)<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u7684\u8bbf\u95ee\u5c06\u88ab\u963b\u585e\uff0c\u8fd9\u8bf4\u660e<code>synchronized<\/code>\u4f7f\u7528\u7684\u201c\u5bf9\u8c61\u76d1\u89c6\u5668\u201d\u662f\u540c\u4e00\u4e2a\u3002<\/p>\n<\/li>\n<li>\n<p>\u548c<code>synchronized<\/code>\u65b9\u6cd5\u4e00\u6837\uff0c<code>synchronized(this)<\/code>\u4ee3\u7801\u5757\u662f\u9501\u5b9a\u5f53\u524d\u5bf9\u8c61\u7684\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u5c06\u4efb\u610f\u5bf9\u8c61\u4f5c\u4e3a\u5bf9\u8c61\u76d1\u89c6\u5668<\/h3>\n<blockquote>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u8c03\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u7684\u4e0d\u540c\u540d\u79f0\u7684<code>synchronized<\/code>\u540c\u6b65\u65b9\u6cd5\u6216<code>synchronized(this)<\/code>\u65f6\uff0c\u8c03\u7528\u6548\u679c\u5c31\u662f\u6309\u987a\u5e8f\u8fdb\u884c\uff0c\u4e5f\u5c31\u662f\u540c\u6b65\uff0c\u963b\u585e\u7684\u3002<\/p>\n<\/blockquote>\n<ul>\n<li>\u65e0\u8bba\u662f<code>synchronized(this)<\/code>\u4ee3\u7801\u5757\u8fd8\u662f<code>synchronized<\/code>\u540c\u6b65\u65b9\u6cd5\uff0c\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c<code>synchronized(this)<\/code>\u4ee3\u7801\u5757\u4e2d\u7684\u4ee3\u7801\u6216<code>synchronized<\/code>\u540c\u6b65\u65b9\u6cd5\uff0c\u5bf9\u5176\u4ed6<code>synchronized(this)<\/code>\u4ee3\u7801\u5757\u4e2d\u7684\u4ee3\u7801\u6216<code>synchronized<\/code>\u540c\u6b65\u65b9\u6cd5\u7684\u8c03\u7528\u90fd\u662f\u5448\u963b\u585e\u72b6\u6001\u7684\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>Java\u4e2d\u652f\u6301\u5bf9\u201c\u4efb\u610f\u5bf9\u8c61\u201d\u4f5c\u4e3a\u201c\u5bf9\u8c61\u76d1\u89c6\u5668\u201d\u6765\u5b9e\u73b0\u540c\u6b65\u7684\u529f\u80fd\u3002\u8fd9\u4e2a\u5bf9\u8c61\u5927\u591a\u662f\u5b9e\u4f8b\u53d8\u91cf\u53ca\u65b9\u6cd5\u4e2d\u7684\u53c2\u6570\uff0c\u4f7f\u7528\u683c\u5f0f<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code><\/p>\n<\/blockquote>\n<ul>\n<li>\u5728\u591a\u4e2a\u7ebf\u7a0b\u6301\u6709\u201c\u5bf9\u8c61\u76d1\u89c6\u5668\u201d\u4f5c\u4e3a\u540c\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u540c\u6b65\u4ee3\u7801\u4e2d\u7684\u4ee3\u7801<\/li>\n<li>\u5f53\u6301\u6709\u201c\u5bf9\u8c61\u76d1\u89c6\u5668\u201d\u4e3a\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u524d\u63d0\u4e0b\uff0c\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u540c\u6b65\u4ee3\u7801\u4e2d\u7684\u4ee3\u7801<\/li>\n<li><code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u4f18\u70b9\uff1a\u5728\u4e00\u4e2a\u7c7b\u4e2d\u6709\u591a\u4e2a<code>synchronized<\/code>\u65b9\u6cd5\uff0c\u8fd9\u65f6\u867d\u7136\u80fd\u5b9e\u73b0\u540c\u6b65\uff0c\u4f46\u662f\u4f1a\u6536\u5230\u963b\u585e\uff0c\u6240\u4ee5\u5f71\u54cd\u6548\u7387\uff1b\u4f46\u5982\u679c\u4f7f\u7528<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u540c\u6b65\u4ee3\u7801\u5757\uff0c\u5219<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u4ee3\u7801\u5757\u4e2d\u7684\u4ee3\u7801\u548c<code>synchronized<\/code>\u540c\u6b65\u65b9\u6cd5\u662f\u5f02\u6b65\u7684\uff0c\u4e0d\u4f1a\u548c\u5176\u4ed6this\u9501\u540c\u6b65\u65b9\u6cd5\u6216\u540c\u6b65\u4ee3\u7801\u5757\u4e89\u62a2this\u9501\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8fd0\u884c\u6548\u7387\u3002<\/li>\n<li><code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u4e2d<code>\u975ethis\u5bf9\u8c61x<\/code>\u4e0d\u540c\u65f6\uff0c\u5219\u662f\u5f02\u6b65\u6548\u679c\u4e0d\u4f1a\u963b\u585e\u3002<\/li>\n<li>\u540c\u6b65\u4ee3\u7801\u5757\u653e\u5728\u975e\u540c\u6b65<code>synchronized<\/code>\u65b9\u6cd5\u4e2d\u58f0\u660e\uff0c\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u8c03\u7528\u65b9\u6cd5\u7684\u7ebf\u7a0b\u7684\u6267\u884c\u540c\u6b65\/\u987a\u5e8f\u6027\uff0c\u4e5f\u5c31\u662f\u7ebf\u7a0b\u8c03\u7528\u65b9\u6cd5\u7684\u987a\u5e8f\u662f\u65e0\u5e8f\u7684\uff0c\u867d\u7136\u540c\u6b65\u5757\u4e2d\u6267\u884c\u7684\u987a\u5e8f\u662f\u540c\u6b65\u7684\uff0c\u8fd9\u6837\u6781\u5bb9\u6613\u51fa\u73b0\u810f\u8bfb\uff0c\u4f7f\u7528<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u7684\u683c\u5f0f\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u810f\u8bfb\u95ee\u9898\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MyService {\n\n    public void serviceMethod(MyList myList, String data) {\n        try {\n            if (myList.getSize() &lt; 1) {\n                Thread.sleep(2000);\/\/\u6a21\u62df\u957f\u65f6\u95f4\u53d6\u6570\u636e\n                myList.add(data);\n            }\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n\n    static class ThreadA extends Thread {\n        private MyList myList;\n\n        ThreadA(MyList myList) {\n            this.myList = myList;\n        }\n\n        @Override\n        public void run() {\n            super.run();\n            new MyService().serviceMethod(myList, &quot;threadA&quot;);\n        }\n    }\n\n    static class ThreadB extends Thread {\n        private MyList myList;\n\n        ThreadB(MyList myList) {\n            this.myList = myList;\n        }\n\n        @Override\n        public void run() {\n            super.run();\n            new MyService().serviceMethod(myList, &quot;threadB&quot;);\n        }\n    }\n\n    static class MyList {\n        private List list = new ArrayList&lt;String&gt;();\n        synchronized public void add(String data) {\n            list.add(data);\n        }\n        synchronized public int getSize() {\n            return list.size();\n        }\n    }\n\n    public static void main(String[] args) throws InterruptedException {\n\n        MyService.MyList myList = new MyService.MyList();\n\n        MyService.ThreadA threadA = new MyService.ThreadA(myList);\n        threadA.setName(&quot;a&quot;);\n        threadA.start();\n\n        MyService.ThreadB threadB = new MyService.ThreadB(myList);\n        threadB.setName(&quot;b&quot;);\n        threadB.start();\n\n        Thread.sleep(6000);\n        System.out.println(&quot;listSize=&quot; + myList.getSize());\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>listSize=2<\/code><\/pre>\n<p>\u7ed3\u679c\u663e\u793a\u51fa\u73b0\u6765\u810f\u8bfb\uff0c\u7a0b\u5e8f\u8131\u79bb\u6765\u6211\u4eec\u7684\u672c\u610f\uff0c\u4f7f\u7528<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u5373\u53ef\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ee3\u7801\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u7531\u4e8emyList\u53c2\u6570\u5bf9\u8c61\u5728\u9879\u76ee\u4e2d\u662f\u4e00\u4efd\u5b9e\u4f8b\uff0c\u5355\u4f8b\u7684\uff0c\u800c\u4e14\u8981\u5bf9myList\u53c2\u6570\u7684getSize()\u65b9\u6cd5\u505a\u540c\u6b65\u7684\u8c03\u7528\uff0c\u6240\u4ee5\u5c31\u5bf9myList\u53c2\u6570\u8fdb\u884c\u540c\u6b65\u5904\u7406\n\/\/\u5728MyServer\u4e2d\u52a0\u5165synchronized(\u975ethis\u5bf9\u8c61x)\uff1a\npublic void serviceMethod(MyList myList, String data) {\n    try {\n        synchronized (myList) {\n            if (myList.getSize() &lt; 1) {\n                Thread.sleep(2000);\/\/\u6a21\u62df\u957f\u65f6\u95f4\u53d6\u6570\u636e\n                myList.add(data);\n            }\n        }\n    } catch (InterruptedException e) {\n        e.printStackTrace();\n    }\n}<\/code><\/pre>\n<h3>\u5bf9synchronized(\u975ethis\u5bf9\u8c61x)\u4e09\u4e2a\u7ed3\u8bba<\/h3>\n<p><code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u683c\u5f0f\u7684\u5199\u6cd5\u662f\u5c06X\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a\u201c\u5bf9\u8c61\u76d1\u89c6\u5668\u201d<\/p>\n<ul>\n<li>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c<code>synchronized(\u975ethis\u5bf9\u8c61x\uff09{}<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u65f6\u5448\u540c\u6b65\u6548\u679c<\/li>\n<li>\u5f53\u5176\u4ed6\u7ebf\u7a0b\u6267\u884cx\u5bf9\u8c61\u4e2d<code>synchronized<\/code>\u540c\u6b65\u65b9\u6cd5\u662f\u5448\u540c\u6b65\u6548\u679c<\/li>\n<li>\u5f53\u5176\u4ed6\u7ebf\u7a0b\u6267\u884cx\u5bf9\u8c61\u65b9\u6cd5\u91cc\u9762\u7684<code>synchronized(this)<\/code>\u4ee3\u7801\u5757\u65f6\u4e5f\u5448\u73b0\u540c\u6b65\u6548\u679c<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528\u4e0d\u52a0<code>synchronized<\/code>\u5173\u952e\u5b57\u7684\u65b9\u6cd5\u6216\u8005\u540c\u6b65\u4ee3\u7801\u5757\u65f6\u8fd8\u662f\u5f02\u6b65\u8c03\u7528<\/p>\n<\/blockquote>\n<h3>\u9759\u6001\u540c\u6b65synchronized\u65b9\u6cd5\u4e0esynchronized(class)\u4ee3\u7801\u5757\u548c\u6570\u636e\u70c8\u6027String\u7684\u5e38\u91cf\u6c60\u6027<\/h3>\n<ul>\n<li>\n<p>\u5173\u952e\u5b57<code>synchronized<\/code>\u8fd8\u53ef\u4ee5\u5e94\u7528\u5728static\u9759\u6001\u65b9\u6cd5\u4e0a\uff0c\u5982\u679c\u8fd9\u6837\u5199\uff0c\u90a3\u662f\u5bf9\u5f53\u524d\u7684<code>*.java<\/code>\u6587\u4ef6\u5bf9<code>Class<\/code>\u7c7b\u8fdb\u884c\u6301\u9501\u3002\u5b83\u548c\u52a0\u5728\u975estatic\u9759\u6001\u65b9\u6cd5\u4e0a\u7684\u548c\u4ee3\u7801\u5757\u7684\u4f5c\u7528\u662f\u4e00\u6837\u7684<\/p>\n<\/li>\n<li>\n<p>\u5728JVM\u4e2d\u5177\u6709String\u5e38\u91cf\u7f13\u5b58\u7684\u529f\u80fd\uff0c\u5c06<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u540c\u6b65\u5757\u4e0eString\u8054\u5408\u4f7f\u7528\u65f6\uff0c\u8981\u6ce8\u610f<code>String a=&quot;AA&quot;; String b=&quot;AA&quot;; \/\/\u7531\u4e8e\u5e38\u91cf\u6c60\u7279\u6b8a\u6027 a\u548cb\u662f\u60f3\u7b49\u7684<\/code>\uff0c\u6240\u4ee5\u5728\u4e24\u4e2a\u7ebf\u7a0b\u5206\u522b\u6301\u6709\u5bf9\u8c61\u9501a\u548c\u5bf9\u8c61\u9501b\u65f6\uff0c\u4ed6\u4eec\u5176\u5b9e\u6301\u6709\u7684\u5bf9\u8c61\u9501\u662f\u540c\u4e00\u4e2a\u201cAA\u201d\uff0c\u6240\u6709\u5728\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u9501\u65f6\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u65f6\u963b\u585e\u7684\u65e0\u6cd5\u6267\u884c\u76f8\u5e94\u7684\u7a0b\u5e8f\u3002\u6240\u4ee5\u5728\u5927\u591a\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u540c\u6b65<code>synchronized(\u975ethis\u5bf9\u8c61x)<\/code>\u4ee3\u7801\u5757\u90fd\u4e0d\u4f7f\u7528String\u4f5c\u4e3a\u9501\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u540c\u6b65\u65b9\u6cd5\u5bb9\u6613\u9020\u6210\u6b7b\u5faa\u73af\u3002\u65e0\u8bba\u662f\u4e24\u4e2a\u6216\u8005\u591a\u4e2a<code>synchronized<\/code>\u65b9\u6cd5\u6709\u5173\u8054\u6216\u65e0\u5173\u8054\uff0c\u82e5\u5176\u4e2d\u4e00\u4e2a<code>synchronized<\/code>\u65b9\u6cd5\u65e0\u9650\u6267\u884c\u4e2d\uff0c\u5c31\u4f1a\u9020\u6210\u5176\u4ed6\u7ebf\u7a0b\u7684\u65e0\u9650\u7b49\u5f85\u3002\u91c7\u7528<code>synchronized<\/code>\u540c\u6b65\u4ee3\u7801\u5757\u6301\u6709\u4e0d\u540c\u7684\u9501\u7c7b\u578b\u5373\u53ef\u89e3\u51b3\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e<code>class A{.....},A a=new A(),synchronized(a){}<\/code>\uff0c\u5982\u679c\u6b64\u65f6\u5bf9\u8c61\u9501a\u672a\u91ca\u653e\uff0c\u800c\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528a\u7684\u540c\u6b65\u975e\u9759\u6001\u65b9\u6cd5\uff0c\u5219\u963b\u585e\u7b49\u5f85\uff0c\u76f4\u5230\u5bf9\u8c61\u9501a\u91ca\u653e\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u9501\u5bf9\u8c61\u7684\u66f4\u6539<\/h3>\n<pre><code class=\"language-java\">public class MyService {\n\n    private String lock = &quot;123&quot;;\n\n    public void serviceMethod() {\n        try {\n            synchronized (lock) {\n                System.out.println(Thread.currentThread().getName() + &quot; begin &quot; + System.currentTimeMillis());\n                lock = &quot;456&quot;;\n                Thread.sleep(2000);\n                System.out.println(Thread.currentThread().getName() + &quot; end &quot; + System.currentTimeMillis());\n            }\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n\n    static class ThreadA extends Thread {\n        private MyService myService;\n\n        ThreadA(MyService myService) {\n            this.myService = myService;\n        }\n\n        @Override\n        public void run() {\n            myService.serviceMethod();\n        }\n    }\n\n    static class ThreadB extends Thread {\n        private MyService myService;\n\n        ThreadB(MyService myService) {\n            this.myService = myService;\n        }\n\n        @Override\n        public void run() {\n            myService.serviceMethod();\n        }\n    }\n\n    public static void main(String[] args) throws InterruptedException {\n        MyService myService = new MyService();\n        MyService.ThreadA threadA = new MyService.ThreadA(myService);\n        threadA.setName(&quot;A&quot;);\n        MyService.ThreadB threadB = new MyService.ThreadB(myService);\n        threadB.setName(&quot;B&quot;);\n        threadA.start();\n        Thread.sleep(50);\n        threadB.start();\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>A begin 1550671515201\nB begin 1550671515256\nA end 1550671517203\nB end 1550671517261<\/code><\/pre>\n<p>\u7531\u4e8e\u7ebf\u7a0b\u963b\u585e\u4e8650\u6beb\u79d2\uff0c\u5bfc\u81f4ThreadB\u8fd0\u884c\u65f6\u6301\u6709\u7684\u9501\u53d8\u6210\u4e86\u201c456\u201d\uff0c\u800cThreadA\u4ecd\u7136\u662f\u201c123\u201d\uff0c\u6240\u4ee5\u4e24\u4e2a\u7ebf\u7a0b\u5448\u5f02\u6b65\u3002\u53bb\u6389\u7761\u772050\u6beb\u79d2\u8fd9\u884c\u4ee3\u7801\u591a\u8fd0\u884c\u51e0\u6b21\uff0c\u53d1\u73b0\u7ed3\u679c\u6709\u65f6\u540c\u6b65\u6709\u65f6\u5f02\u6b65\uff0c\u5e76\u4e0d\u4e00\u5b9a\uff0c\u4e5f\u5c31\u662f\u8bf4\u548c\u53bb\u4e0d\u53bb\u638950\u6beb\u79d2\u7684\u5173\u7cfb\u5e76\u4e0d\u8d77\u51b3\u5b9a\u884c\u4f5c\u7528\uff0c\u53ea\u8981\u7ebf\u7a0bThreadB\u5728lock\u53d8\u6210\u201c456\u201d\u4e4b\u524d\u8fd0\u884c\uff0c\u90a3\u4e48\u7ed3\u679c\u5c31\u662f\u540c\u6b65\u5219\u4e24\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u7684\u9501\u90fd\u4e3a\u201cA\u201d\uff0c\u5728\u5176\u4e4b\u540e\u5c31\u662f\u5f02\u6b65\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u7684\u9501\u5206\u522b\u4e3a\u201c123\u201d\u548c\u201c456\u201d\uff0c\u8fd9\u4e5f\u662f\u7ebf\u7a0b\u8fd0\u884c\u968f\u673a\u6027\u9020\u6210\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>synchronized\u540c\u6b65\u65b9\u6cd5 \u65b9\u6cd5\u5185\u7684\u53d8\u91cf\u4e3a\u7ebf\u7a0b\u5b89\u5168 \u201c\u975e\u7ebf\u7a0b\u5b89\u5168\u201d\u95ee\u9898\u5b58\u5728\u4e0e\u5b9e\u4f8b\u53d8\u91cf\u4e2d\uff0c\u5982\u679c\u662f\u65b9\u6cd5\u5185\u90e8\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[51],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-multi-threads"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}