{"id":409,"date":"2024-10-23T11:16:12","date_gmt":"2024-10-23T03:16:12","guid":{"rendered":"https:\/\/www.zackzzp.top\/?p=409"},"modified":"2024-10-28T20:54:24","modified_gmt":"2024-10-28T12:54:24","slug":"%e4%b8%80%e6%96%87%e4%ba%86%e8%a7%a3aqs","status":"publish","type":"post","link":"http:\/\/www.zackzzp.top\/index.php\/2024\/10\/23\/%e4%b8%80%e6%96%87%e4%ba%86%e8%a7%a3aqs\/","title":{"rendered":"\u4e00\u6587\u4e86\u89e3AQS"},"content":{"rendered":"<h2>\u524d\u7f6e\u77e5\u8bc6\uff08LockSupport\uff09<\/h2>\n<h3>\u7b80\u4ecb<\/h3>\n<p><strong>LockSupport\u662f\u7528\u6765\u521b\u5efa\u9501\u548c\u5176\u4ed6\u540c\u6b65\u5de5\u5177\u7c7b\u7684\u57fa\u672c\u7ebf\u7a0b\u963b\u585e\u539f\u8bed\u3002<\/strong><br \/>java\u9501\u548c\u540c\u6b65\u5668\u6846\u67b6\u7684\u6838\u5fc3 AQS: AbstractQueuedSynchronizer\uff0c\u5c31\u662f\u901a\u8fc7\u8c03\u7528 LockSupport .park()\u548c LockSupport .unpark()\u5b9e\u73b0\u7ebf\u7a0b\u7684\u963b\u585e\u548c\u5524\u9192 \u7684\u3002 LockSupport \u5f88<strong>\u7c7b\u4f3c\u4e8e\u4e8c\u5143\u4fe1\u53f7\u91cf<\/strong>(\u53ea\u67091\u4e2a\u8bb8\u53ef\u8bc1\u53ef\u4f9b\u4f7f\u7528)\uff0c\u5982\u679c\u8fd9\u4e2a\u8bb8\u53ef\u8fd8\u6ca1\u6709\u88ab\u5360\u7528\uff0c\u5f53\u524d\u7ebf\u7a0b\u83b7\u53d6\u8bb8\u53ef\u5e76\u7ee7 \u7eed \u6267\u884c\uff1b\u5982\u679c\u8bb8\u53ef\u5df2\u7ecf\u88ab\u5360\u7528\uff0c\u5f53\u524d\u7ebf \u7a0b\u963b\u585e\uff0c\u7b49\u5f85\u83b7\u53d6\u8bb8\u53ef\u3002<\/p>\n<p><strong>LockSupport\u7c7b\u5c5e\u6027<\/strong><\/p>\n<pre class=\"code\">public class LockSupport {\n    \/\/ Hotspot implementation via intrinsics API\n    private static final sun.misc.Unsafe UNSAFE;\n    \/\/ \u8868\u793a\u5185\u5b58\u504f\u79fb\u5730\u5740\n    private static final long parkBlockerOffset;\n    \/\/ \u8868\u793a\u5185\u5b58\u504f\u79fb\u5730\u5740\n    private static final long SEED;\n    \/\/ \u8868\u793a\u5185\u5b58\u504f\u79fb\u5730\u5740\n    private static final long PROBE;\n    \/\/ \u8868\u793a\u5185\u5b58\u504f\u79fb\u5730\u5740\n    private static final long SECONDARY;\n    \n    static {\n        try {\n            \/\/ \u83b7\u53d6Unsafe\u5b9e\u4f8b\n            UNSAFE = sun.misc.Unsafe.getUnsafe();\n            \/\/ \u7ebf\u7a0b\u7c7b\u7c7b\u578b\n            Class&lt;?&gt; tk = Thread.class;\n            \/\/ \u83b7\u53d6Thread\u7684parkBlocker\u5b57\u6bb5\u7684\u5185\u5b58\u504f\u79fb\u5730\u5740\n            parkBlockerOffset = UNSAFE.objectFieldOffset\n                (tk.getDeclaredField(\"parkBlocker\"));\n            \/\/ \u83b7\u53d6Thread\u7684threadLocalRandomSeed\u5b57\u6bb5\u7684\u5185\u5b58\u504f\u79fb\u5730\u5740\n            SEED = UNSAFE.objectFieldOffset\n                (tk.getDeclaredField(\"threadLocalRandomSeed\"));\n            \/\/ \u83b7\u53d6Thread\u7684threadLocalRandomProbe\u5b57\u6bb5\u7684\u5185\u5b58\u504f\u79fb\u5730\u5740\n            PROBE = UNSAFE.objectFieldOffset\n                (tk.getDeclaredField(\"threadLocalRandomProbe\"));\n            \/\/ \u83b7\u53d6Thread\u7684threadLocalRandomSecondarySeed\u5b57\u6bb5\u7684\u5185\u5b58\u504f\u79fb\u5730\u5740\n            SECONDARY = UNSAFE.objectFieldOffset\n                (tk.getDeclaredField(\"threadLocalRandomSecondarySeed\"));\n        } catch (Exception ex) { throw new Error(ex); }\n    }\n}\n\/\/ \u79c1\u6709\u6784\u9020\u51fd\u6570\uff0c\u65e0\u6cd5\u88ab\u5b9e\u4f8b\u5316\nprivate LockSupport() {} <\/pre>\n<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h3>\u4e09\u79cd\u8ba9\u7ebf\u7a0b\u7b49\u5f85\u548c\u5524\u9192\u7684\u65b9\u6cd5<\/h3>\n<h4><strong>1.\u4f7f\u7528wait\u548cnotify<\/strong><\/h4>\n<pre class=\"code\">\u00a0public class ObjectWait {\n\n    public static void main(String[] args) {\n        Object o = new Object();\n\n\n        Thread t = new Thread(new Runnable() {\n            @Override\n            public void run() {\n\n                System.out.println(\"\u7ebf\u7a0bA\u88abo.wait()\u963b\u585e\u524d\");\n                synchronized(o){\n                    try {\n                        o.wait();\n                    } catch (InterruptedException e) {\n                        e.printStackTrace();\n                    }\n\n                }\n                System.out.println(\"\u7ebf\u7a0bA\u88ab\u7ebf\u7a0bB o.notify()\u5524\u9192\");\n\n            }\n        },\"A\");\n\n        t.start();\n\n\n        try {\n            Thread.sleep(100);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n\n        new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(\"\u7ebf\u7a0bB\u5524\u9192\u7ebf\u7a0bA\");\n                synchronized (o){\n                    o.notify();\n                }\n            }\n        },\"B\").start();\n    }\n}\n\n\u7ebf\u7a0bA\u88abo.wait()\u963b\u585e\u524d\n\u7ebf\u7a0bB\u5524\u9192\u7ebf\u7a0bA\n\u7ebf\u7a0bA\u88ab\u7ebf\u7a0bB o.notify()\u5524\u9192\n<\/pre>\n<h4><strong>2.\u4f7f\u7528lock.condition<\/strong><\/h4>\n<pre class=\"code\">public class ConditionAwait {\n\n    public static void main(String[] args) {\n\n        Lock lock = new ReentrantLock();\n        Condition condition = lock.newCondition();\n\n        new Thread(new Runnable() {\n            @Override\n            public void run() {\n\n                System.out.println(\"\u7ebf\u7a0bA\u88abcondition.await()\u963b\u585e\u524d\");\n\n                try {\n                    lock.lock();\n                    condition.await();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }finally {\n                    lock.unlock();\n                }\n                System.out.println(\"\u7ebf\u7a0bA\u88ab\u7ebf\u7a0bB condition.signl()\u5524\u9192\");\n            }\n        }, \"A\").start();\n\n\n        new Thread(new Runnable() {\n            @Override\n            public void run() {\n\n\n\n                try {\n                    lock.lock();\n\n                    System.out.println(\"\u7ebf\u7a0bB\u4e2d\u4f7f\u7528condition.signal()\u5524\u9192\u7ebf\u7a0bA\");\n                    condition.signal();\n                }catch (Exception e){\n\n\n                }finally {\n                    lock.unlock();\n                }\n\n            }\n        }, \"B\").start();\n\n    }\n}\n\n\u7ed3\u679c\uff1a\n\u7ebf\u7a0bA\u88abcondition.await()\u963b\u585e\u524d\n\u7ebf\u7a0bB\u4e2d\u4f7f\u7528condition.signal()\u5524\u9192\u7ebf\u7a0bA\n\u7ebf\u7a0bA\u88ab\u7ebf\u7a0bB condition.signl()\u5524\u9192 <\/pre>\n<h4><strong>3.\u4f7f\u7528locksupport<\/strong><\/h4>\n<pre class=\"code\">\u00a0public class LockSupportDemo {\n\n    public static void main(String[] args) {\n\n        Thread t = new Thread(new Runnable() {\n            @Override\n            public void run() {\n\n                System.out.println(\"\u7ebf\u7a0bA\u88abLockSupport.park()\u963b\u585e\");\n                LockSupport.park();\n\n                System.out.println(\"\u7ebf\u7a0bA\u88ab\u7ebf\u7a0bB LockSupport.unpark()\u5524\u9192\");\n\n            }\n        },\"A\");\n\n        t.start();\n        \n        new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(\"\u7ebf\u7a0bB\u5524\u9192\u7ebf\u7a0bA\");\n                \/\/ \u5524\u9192\u6307\u5b9a\u7ebf\u7a0bt\uff0c\u4e5f\u5c31\u662fA\n                LockSupport.unpark(t);\n            }\n        },\"B\").start();\n    }\n}\n\n\u7ed3\u679c\uff1a\n\u7ebf\u7a0bA\u88abLockSupport.park()\u963b\u585e\n\u7ebf\u7a0bB\u5524\u9192\u7ebf\u7a0bA\n\u7ebf\u7a0bA\u88ab\u7ebf\u7a0bB LockSupport.unpark()\u5524\u9192<\/pre>\n<p><strong>\u4f7f\u7528interrupt\u4e2d\u65ad\u963b\u585e<\/strong><\/p>\n<pre class=\"code\">\u00a0package CompleteFuture;\n\nimport java.util.concurrent.locks.LockSupport;\n\npublic class LockSupportDemo {\n\n    public static void main(String[] args) {\n\n        Thread t = new Thread(new Runnable() {\n            @Override\n            public void run() {\n\n                System.out.println(\"before park\");\n                LockSupport.park();\n                System.out.println(\"after park\");\n\n            }\n        },\"A\");\n\n        t.start();\n\n\n       \/\/\u786e\u4fdd park()\u6267\u884c\n        try {\n            Thread.sleep(3000);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n\/\/        System.out.println(\"\u7ebf\u7a0bt\u662f\u5426\u88ab\u963b\u585e: \"+t.isInterrupted());\n        System.out.println(\"before interrupted\");\n        t.interrupt();\n        System.out.println(\"after interrupted\");\n\n    }\n}\n\n\u7ed3\u679c\uff1a\nbefore park\nbefore interrupted\nafter interrupted\nafter park<\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/23\/image30d42422028aee3e.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/23\/image30d42422028aee3e.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h3>LockSupport\u6e90\u7801\u5206\u6790<\/h3>\n<p><strong>1.park\uff08\uff09<\/strong><\/p>\n<pre class=\"code\">\/**Disables the current thread for thread scheduling purposes unless the permit is available.\nIf the permit is available then it is consumed and the call returns immediately; otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:\nSome other thread invokes unpark with the current thread as the target; or\nSome other thread interrupts the current thread; or\nThe call spuriously (that is, for no reason) returns.\nThis method does not report which of these caused the method to return. Callers should re-check the conditions which caused the thread to park in the first place. Callers may also determine, for example, the interrupt status of the thread upon return. \n*\/\npublic static void park() {\n        UNSAFE.park(false, 0L);\n    } <\/pre>\n<p>\u5982\u679c\u6ca1\u6709permit\u8bb8\u53ef\uff0c\u90a3\u4e48\u8c03\u7528\u8be5\u65b9\u6cd5\u540e\uff0c\u5f53\u524d\u7ebf\u7a0b\u7acb\u9a6c\u505c\u6b62\u6267\u884c\u8ba1\u5212\uff08\u963b\u585e\uff09\uff0c\u76f4\u5230\u6709\u4e00\u4e0b3\u4e2d\u60c5\u51b5\u53d1\u751f\uff1a<br \/>1\u3001\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528unpark(\u88ab\u963b\u585e\u7ebf\u7a0b\u5f15\u7528)\u65b9\u6cd5\uff0c\u53c2\u6570\u4e3a\u9700\u8981\u5524\u9192\u7684\u7ebf\u7a0b\uff1b<br \/>2\u3001\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u65ad\u5f53\u524d\u7ebf\u7a0b\uff1b<br \/>3\u3001\u8c03\u7528\u865a\u5047\uff08\u5373\u65e0\u7f18\u65e0\u6545\uff09\u8fd4\u56de\uff1b<\/p>\n<p>UNSAFE.park(isAbsolute,timeout)\u7684\u7406\u89e3\uff0c\u963b\u585e\u4e00\u4e2a\u7ebf\u7a0b\u76f4\u5230unpark\u51fa\u73b0\u3001\u7ebf\u7a0b<\/p>\n<p>\u88ab\u4e2d\u65ad\u6216\u8005timeout\u65f6\u95f4\u5230\u671f\u3002\u5982\u679c\u4e00\u4e2aunpark\u8c03\u7528\u5df2\u7ecf\u51fa\u73b0\u4e86\uff0c<\/p>\n<p>\u8fd9\u91cc\u53ea\u8ba1\u6570\u3002timeout\u4e3a0\u8868\u793a\u6c38\u4e0d\u8fc7\u671f.\u5f53isAbsolute\u4e3atrue\u65f6\uff0c<\/p>\n<p>timeout\u662f\u76f8\u5bf9\u4e8e\u65b0\u7eaa\u5143\u4e4b\u540e\u7684\u6beb\u79d2\u3002\u5426\u5219\u8fd9\u4e2a\u503c\u5c31\u662f\u8d85\u65f6\u524d\u7684\u7eb3\u79d2\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u6267\u884c\u65f6<\/p>\n<p>\u4e5f\u53ef\u80fd\u4e0d\u5408\u7406\u5730\u8fd4\u56de(\u6ca1\u6709\u5177\u4f53\u539f\u56e0)<\/p>\n<p><strong>2.unpark\uff08<\/strong>Thread thread<strong>)<\/strong><\/p>\n<pre class=\"code\">\u00a0 public static void unpark(Thread thread) {\n        if (thread != null)\n            UNSAFE.unpark(thread);\n    }<\/pre>\n<p>\u7ed9\u6307\u5b9a\u7684\u7ebf\u7a0b\u63d0\u4f9bunblock\u51ed\u8bc1\u3002\u5982\u679c\u6307\u5b9a\u7684\u7ebf\u7a0b\u4f7f\u7528\u4e86park()\uff0c\u5219\u7ebf\u7a0b\u53d8\u6210\u975e\u963b\u585e\u3002\u5982\u679c\u6ca1\u6709\u4f7f\u7528park\uff0c\u5219\u7ebf\u7a0b\u4e0b\u4e00\u6b21\u4f7f\u7528park\u65f6\uff0c\u7ebf\u7a0b\u4e0d\u4f1a\u963b\u585e\u3002<\/p>\n<p><strong>park\uff08blocker\uff09\u9501\u5b9a\u6307\u5b9a\u5bf9\u8c61<\/strong><\/p>\n<pre class=\"code\">\u00a0public static void park(Object blocker) {\n    \/\/ \u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\n    Thread t = Thread.currentThread();\n    \/\/ \u8bbe\u7f6eBlocker\n    setBlocker(t, blocker);\n    \/\/ \u83b7\u53d6\u8bb8\u53ef\n    UNSAFE.park(false, 0L);\n    \/\/ \u91cd\u65b0\u53ef\u8fd0\u884c\u540e\u518d\u6b64\u8bbe\u7f6eBlocker\n    setBlocker(t, null);\n}<\/pre>\n<p>\u8c03\u7528park\u51fd\u6570\u65f6\uff0c\u9996\u5148\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\uff0c\u7136\u540e\u8bbe\u7f6e\u5f53\u524d\u7ebf\u7a0b\u7684parkBlocker\u5b57\u6bb5\uff0c\u5373\u8c03\u7528setBlocker\u51fd\u6570\uff0c\u4e4b\u540e\u8c03\u7528Unsafe\u7c7b\u7684park\u51fd\u6570\uff0c\u4e4b\u540e\u518d\u8c03\u7528setBlocker\u51fd\u6570\u3002\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4e3a\u4ec0\u4e48\u8981\u5728\u6b64park\u51fd\u6570\u4e2d\u8981\u8c03\u7528\u4e24\u6b21setBlocker\u51fd\u6570\u5462? \u539f\u56e0\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u8c03\u7528park\u51fd\u6570\u65f6\uff0c\u5f53\u524d\u7ebf\u7a0b\u9996\u5148\u8bbe\u7f6e\u597dparkBlocker\u5b57\u6bb5\uff0c\u7136\u540e\u518d\u8c03\u7528Unsafe\u7684park\u51fd\u6570\uff0c\u6b64\u540e\uff0c\u5f53\u524d\u7ebf\u7a0b\u5c31\u5df2\u7ecf\u963b\u585e\u4e86\uff0c\u7b49\u5f85\u8be5\u7ebf\u7a0b\u7684unpark\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u6240\u4ee5\u540e\u9762\u7684\u4e00\u4e2asetBlocker\u51fd\u6570\u65e0\u6cd5\u8fd0\u884c\uff0cunpark\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u8be5\u7ebf\u7a0b\u83b7\u5f97\u8bb8\u53ef\u540e\uff0c\u5c31\u53ef\u4ee5\u7ee7\u7eed\u8fd0\u884c\u4e86\uff0c\u4e5f\u5c31\u8fd0\u884c\u7b2c\u4e8c\u4e2asetBlocker\uff0c\u628a\u8be5\u7ebf\u7a0b\u7684parkBlocker\u5b57\u6bb5\u8bbe\u7f6e\u4e3anull\uff0c\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u6574\u4e2apark\u51fd\u6570\u7684\u903b\u8f91\u3002\u5982\u679c\u6ca1\u6709\u7b2c\u4e8c\u4e2asetBlocker\uff0c\u90a3\u4e48\u4e4b\u540e\u6ca1\u6709\u8c03\u7528park(Object blocker)\uff0c\u800c\u76f4\u63a5\u8c03\u7528getBlocker\u51fd\u6570\uff0c\u5f97\u5230\u7684\u8fd8\u662f\u524d\u4e00\u4e2apark(Object blocker)\u8bbe\u7f6e\u7684blocker\uff0c\u663e\u7136\u662f\u4e0d\u7b26\u5408\u903b\u8f91\u7684\u3002\u603b\u4e4b\uff0c\u5fc5\u987b\u8981\u4fdd\u8bc1\u5728park(Object blocker)\u6574\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u540e\uff0c\u8be5\u7ebf\u7a0b\u7684parkBlocker\u5b57\u6bb5\u53c8\u6062\u590d\u4e3anull\u3002\u6240\u4ee5\uff0cpark(Object)\u578b\u51fd\u6570\u91cc\u5fc5\u987b\u8981\u8c03\u7528setBlocker\u51fd\u6570\u4e24\u6b21\u3002<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h3>\u4e09\u79cd\u963b\u585e\u65b9\u6cd5\u7684\u533a\u522b<\/h3>\n<h4><strong>Thread.sleep()\u548cObject.wait()\u7684\u533a\u522b<\/strong><\/h4>\n<p>Thread.sleep()\u4e0d\u4f1a\u91ca\u653e\u5360\u6709\u7684\u9501\uff0cObject.wait()\u4f1a\u91ca\u653e\u5360\u6709\u7684\u9501\uff1b<\/p>\n<h4><strong>Object.wait()\u548cCondition.await()\u7684\u533a\u522b<\/strong><\/h4>\n<p>Object.wait()\u548cCondition.await()\u7684\u539f\u7406\u662f\u57fa\u672c\u4e00\u81f4\u7684\uff0c\u4e0d\u540c\u7684\u662fCondition.await()\u5e95\u5c42\u662f\u8c03\u7528LockSupport.park()\u6765\u5b9e\u73b0\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u7684\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u5b83\u5728\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u4e4b\u524d\u8fd8\u5e72\u4e86\u4e24\u4ef6\u4e8b\uff0c\u4e00\u662f\u628a\u5f53\u524d\u7ebf\u7a0b\u6dfb\u52a0\u5230\u6761\u4ef6\u961f\u5217\u4e2d\uff0c\u4e8c\u662f\u201c\u5b8c\u5168\u201d\u91ca\u653e\u9501\uff0c\u4e5f\u5c31\u662f\u8ba9state\u72b6\u6001\u53d8\u91cf\u53d8\u4e3a0\uff0c\u7136\u540e\u624d\u662f\u8c03\u7528LockSupport.park()\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u3002<\/p>\n<h4><strong>Thread.sleep()\u548cLockSupport.park()\u7684\u533a\u522b<\/strong><\/h4>\n<p>LockSupport.park()\u8fd8\u6709\u51e0\u4e2a\u5144\u5f1f\u65b9\u6cd5\u2014\u2014parkNanos()\u3001parkUtil()\u7b49\uff0c\u6211\u4eec\u8fd9\u91cc\u8bf4\u7684park()\u65b9\u6cd5\u7edf\u79f0\u8fd9\u4e00\u7c7b\u65b9\u6cd5\u3002<br \/>\u4ece\u529f\u80fd\u4e0a\u6765\u8bf4\uff0cThread.sleep()\u548cLockSupport.park()\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u90fd\u662f\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u7684\u6267\u884c\uff0c\u4e14\u90fd\u4e0d\u4f1a\u91ca\u653e\u5f53\u524d\u7ebf\u7a0b\u5360\u6709\u7684\u9501\u8d44\u6e90\uff1b<\/p>\n<p>Thread.sleep()\u6ca1\u6cd5\u4ece\u5916\u90e8\u5524\u9192\uff0c\u53ea\u80fd\u81ea\u5df1\u9192\u8fc7\u6765\uff1b<\/p>\n<p>LockSupport.park()\u65b9\u6cd5\u53ef\u4ee5\u88ab\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528LockSupport.unpark()\u65b9\u6cd5\u5524\u9192\uff1b<\/p>\n<p>Thread.sleep()\u65b9\u6cd5\u58f0\u660e\u4e0a\u629b\u51fa\u4e86InterruptedException\u4e2d\u65ad\u5f02\u5e38\uff0c\u6240\u4ee5\u8c03\u7528\u8005\u9700\u8981\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u6216\u8005\u518d\u629b\u51fa\uff1b<\/p>\n<p>LockSupport.park()\u65b9\u6cd5\u4e0d\u9700\u8981\u6355\u83b7\u4e2d\u65ad\u5f02\u5e38\uff1b<\/p>\n<p>Thread.sleep()\u672c\u8eab\u5c31\u662f\u4e00\u4e2anative\u65b9\u6cd5\uff1b LockSupport.park()\u5e95\u5c42\u662f\u8c03\u7528\u7684Unsafe\u7684native\u65b9\u6cd5\uff1b<\/p>\n<h4><strong>Object.wait()\u548cLockSupport.park()\u7684\u533a\u522b \u4e8c\u8005\u90fd\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u7684\u8fd0\u884c\uff0c\u4ed6\u4eec\u6709\u4ec0\u4e48\u533a\u522b\u5462?<\/strong><\/h4>\n<p>Object.wait()\u65b9\u6cd5\u9700\u8981\u5728synchronized\u5757\u4e2d\u6267\u884c\uff1b LockSupport.park()\u53ef\u4ee5\u5728\u4efb\u610f\u5730\u65b9\u6267\u884c\uff1b<br \/>Object.wait()\u65b9\u6cd5\u58f0\u660e\u629b\u51fa\u4e86\u4e2d\u65ad\u5f02\u5e38\uff0c\u8c03\u7528\u8005\u9700\u8981\u6355\u83b7\u6216\u8005\u518d\u629b\u51fa<br \/>LockSupport.park()\u4e0d\u9700\u8981\u6355\u83b7\u4e2d\u65ad\u5f02\u5e38\uff1b<\/p>\n<p>Object.wait()\u4e0d\u5e26\u8d85\u65f6\u7684\uff0c\u9700\u8981\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cnotify()\u6765\u5524\u9192\uff0c\u4f46\u4e0d\u4e00\u5b9a\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u5185\u5bb9\uff1b<br \/>LockSupport.park()\u4e0d\u5e26\u8d85\u65f6\u7684\uff0c\u9700\u8981\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cunpark()\u6765\u5524\u9192\uff0c\u4e00\u5b9a\u4f1a\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u5185\u5bb9\uff1b<\/p>\n<p>park()\/unpark()\u5e95\u5c42\u7684\u539f\u7406\u662f\u201c\u4e8c\u5143\u4fe1\u53f7\u91cf\u201d\uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u76f8\u50cf\u6210\u53ea\u6709\u4e00\u4e2a\u8bb8\u53ef\u8bc1\u7684Semaphore\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u4e2a\u4fe1\u53f7\u91cf\u5728\u91cd\u590d\u6267\u884cunpark()\u7684\u65f6\u5019\u4e5f\u4e0d\u4f1a\u518d\u589e\u52a0\u8bb8\u53ef\u8bc1\uff0c\u6700\u591a\u53ea\u6709\u4e00\u4e2a\u8bb8\u53ef\u8bc1\u3002<\/p>\n<h4><strong>\u00a0\u5982\u679c\u5728wait()\u4e4b\u524d\u6267\u884c\u4e86notify()\u4f1a\u600e\u6837?<\/strong><\/h4>\n<p>\u5982\u679c\u5f53\u524d\u7684\u7ebf\u7a0b\u4e0d\u662f\u6b64\u5bf9\u8c61\u9501\u7684\u6240\u6709\u8005\uff0c\u5374\u8c03\u7528\u8be5\u5bf9\u8c61\u7684notify()\u6216wait()\u65b9\u6cd5\u65f6\u629b\u51faIllegalMonitorStateException\u5f02\u5e38\uff1b<\/p>\n<p>\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u662f\u6b64\u5bf9\u8c61\u9501\u7684\u6240\u6709\u8005\uff0cwait()\u5c06\u4e00\u76f4\u963b\u585e\uff0c\u56e0\u4e3a\u540e\u7eed\u5c06\u6ca1\u6709\u5176\u5b83notify()\u5524\u9192\u5b83\u3002<\/p>\n<h4><strong>\u5982\u679c\u5728park()\u4e4b\u524d\u6267\u884c\u4e86unpark()\u4f1a\u600e\u6837?<\/strong><\/h4>\n<p>\u7ebf\u7a0b\u4e0d\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u63a5\u8df3\u8fc7park()\uff0c\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u5185\u5bb9<\/p>\n<\/p>\n<h4><strong>LockSupport.park()\u4f1a\u91ca\u653e\u9501\u8d44\u6e90\u5417?<\/strong><\/h4>\n<p>\u4e0d\u4f1a\uff0c\u5b83\u53ea\u8d1f\u8d23\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\uff0c\u91ca\u653e\u9501\u8d44\u6e90\u5b9e\u9645\u4e0a\u662f\u5728Condition\u7684await()\u65b9\u6cd5\u4e2d\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2>AQS<\/h2>\n<h3>\u7b80\u4ecb<\/h3>\n<p>AQS\u662f\u4e00\u4e2a\u7528\u6765\u6784\u5efa\u9501\u548c\u540c\u6b65\u5668\u7684\u6846\u67b6\uff0c\u4f7f\u7528AQS\u80fd\u7b80\u5355\u4e14\u9ad8\u6548\u5730\u6784\u9020\u51fa\u5e94\u7528\u5e7f\u6cdb\u7684\u5927\u91cf\u7684\u540c\u6b65\u5668\uff0c\u6bd4\u5982\u6211\u4eec\u63d0\u5230\u7684ReentrantLock\uff0cSemaphore\uff0c\u5176\u4ed6\u7684\u8bf8\u5982ReentrantReadWriteLock\uff0cSynchronousQueue\uff0cFutureTask\u7b49\u7b49\u7686\u662f\u57fa\u4e8eAQS\u7684\u3002\u5f53\u7136\uff0c\u6211\u4eec\u81ea\u5df1\u4e5f\u80fd\u5229\u7528AQS\u975e\u5e38\u8f7b\u677e\u5bb9\u6613\u5730\u6784\u9020\u51fa\u7b26\u5408\u6211\u4eec\u81ea\u5df1\u9700\u6c42\u7684\u540c\u6b65\u5668\u3002<\/p>\n<h3>AQS\u6838\u5fc3\u601d\u60f3<\/h3>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/23\/imaged6ee0b621f0e6e1c.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/23\/imaged6ee0b621f0e6e1c.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n<h3>AQS\u6e90\u7801\u5206\u6790<\/h3>\n<pre class=\"code\">\u00a0AQS \u4e3b\u8981\u6709\u4e09\u5927\u5c5e\u6027\u5206\u522b\u662f head ,tail, state,\u5176\u4e2dstate \u8868\u793a\u540c\u6b65\u72b6\u6001\uff0chead\u4e3a\u7b49\u5f85\u961f\u5217\u7684\u5934\u7ed3\u70b9\uff0ctail \u6307\u5411\u961f\u5217\u7684\u5c3e\u8282\u70b9\u3002\n    \/**\n     * Head of the wait queue, lazily initialized.  Except for\n     * initialization, it is modified only via method setHead.  Note:\n     * If head exists, its waitStatus is guaranteed not to be\n     * CANCELLED.\n     *\/\n    private transient volatile Node head;\n\n    \/**\n     * Tail of the wait queue, lazily initialized.  Modified only via\n     * method enq to add new wait node.\n     *\/\n    private transient volatile Node tail;\n\n    \/**\n     * The synchronization state.\n     *\/\n    private volatile int state;\n\n\u5728\u8fd9\u91cc\u63d2\u5165\u4ee3\u7801\u7247\nclass Node{\n  \/\/\u8282\u70b9\u7b49\u5f85\u72b6\u6001\n  volatile int waitStatus;\n  \/\/ \u53cc\u5411\u94fe\u8868\u5f53\u524d\u8282\u70b9\u524d\u8282\u70b9\n  volatile Node prev;\n  \/\/ \u4e0b\u4e00\u4e2a\u8282\u70b9\n  volatile Node next;\n  \/\/ \u5f53\u524d\u8282\u70b9\u5b58\u653e\u7684\u7ebf\u7a0b\n  volatile Thread thread;\n  \/\/ condition\u6761\u4ef6\u7b49\u5f85\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n  Node nextWaiter;\n}<\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/23\/image6e980fb828f2c93c.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/23\/image6e980fb828f2c93c.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n<hr \/>\n<h4>lock\uff08\uff09\u6e90\u7801\u5206\u6790<\/h4>\n<pre class=\"code\">final void lock() {\n            \/\/1\u3001\u5224\u65ad\u5f53\u524dstate \u72b6\u6001, \u6ca1\u6709\u9501\u5219\u5f53\u524d\u7ebf\u7a0b\u62a2\u5360\u9501\n            if (compareAndSetState(0, 1))\n                \/\/ \u72ec\u5360\u9501\n                setExclusiveOwnerThread(Thread.currentThread());\n            else\n                \/\/ 2\u3001\u9501\u88ab\u4eba\u5360\u4e86\uff0c\u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u5173\u952e\u65b9\u6cd5\u4e86\n                acquire(1);\n        }\n\n  public final void acquire(int arg) {\n        if (!tryAcquire(arg) &amp;&amp;\n            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))\n            selfInterrupt();\n    }<\/pre>\n<p><strong>tryacquire\u5206\u6790<\/strong><\/p>\n<pre class=\"code\">\u00a0  final boolean nonfairTryAcquire(int acquires) {\n            \/\/1\u3001\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\n            final Thread current = Thread.currentThread();\n            \/\/ 2\u3001\u83b7\u53d6\u5f53\u524d\u9501\u7684\u72b6\u6001\uff0c0 \u8868\u793a\u6ca1\u6709\u88ab\u7ebf\u7a0b\u5360\u6709\uff0c&gt;0 \u8868\u793a\u9501\u88ab\u522b\u7684\u7ebf\u7a0b\u5360\u6709\n            int c = getState();\n            \/\/ 3\u3001\u5982\u679c\u9501\u6ca1\u6709\u88ab\u7ebf\u7a0b\u5360\u6709\n            if (c == 0) {\n                 \/\/ 3.1\u3001 \u4f7f\u7528CAS\u53bb\u83b7\u53d6\u9501\uff0c   \u4e3a\u4ec0\u4e48\u7528case\u5462\uff0c\u9632\u6b62\u5728\u83b7\u53d6c\u4e4b\u540e c\u7684\u72b6\u6001\u88ab\u4fee\u6539\u4e86\uff0c\u4fdd\u8bc1\u539f\u5b50\u6027\n                if (compareAndSetState(0, acquires)) {\n                    \/\/ 3.2\u3001\u8bbe\u7f6e\u72ec\u5360\u9501\n                    setExclusiveOwnerThread(current);\n                    \/\/ 3.3\u3001\u5f53\u524d\u7ebf\u7a0b\u83b7\u53d6\u5230\u9501\u540e\uff0c\u76f4\u63a5\u53d1\u6325true\n                    return true;\n                }\n            }\n            \/\/\u30004\u3001\u5224\u65ad\u5f53\u524d\u5360\u6709\u9501\u7684\u7ebf\u7a0b\u662f\u4e0d\u662f\u81ea\u5df1\n            else if (current == getExclusiveOwnerThread()) {\n                \/\/ 4.1 \u53ef\u91cd\u5165\u9501\uff0c\u52a0+1\n                int nextc = c + acquires;\n                if (nextc &lt; 0) \/\/ overflow\n                    throw new Error(\"Maximum lock count exceeded\");\n                 \/\/ 4.2 \u8bbe\u7f6e\u9501\u7684\u72b6\u6001\n                setState(nextc);\n                return true;\n            }\n            return false;\n        }<\/pre>\n<p><strong>addWaiter\u65b9\u6cd5\u5206\u6790<\/strong><\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u662f\u4e3a\u4e86\u5c06\u963b\u585e\u7ebf\u7a0b\u52a0\u5165CLH\u961f\u5217\uff0c\u5f53CLH\u961f\u5217\u4e2d\u6709\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u76f4\u63a5\u5728\u5c3e\u90e8\u52a0\u4e0a\uff0c\u4f46\u662f\u5982\u679c\u8be5\u7ebf\u7a0b\u662f\u4e00\u4e2a\u88ab\u963b\u585e\u7684\u7ebf\u7a0b\uff0c\u90a3\u5c31\u8981\u5bf9CLH\u961f\u5217\u521d\u59cb\u5316\uff0c\u8003\u8651\u5230\u591a\u7ebf\u7a0b\u7684\u60c5\u51b5\uff0c\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u6709\u4e00\u4e2a\u5173\u952e\u64cd\u4f5c\u662f\u4f7f\u7528for\u5faa\u73af\u521d\u59cb\u5316<\/p>\n<pre class=\"code\"> private Node addWaiter(Node mode) {\n \t\t\/\/ 1\u3001\u521d\u59cb\u5316\u5f53\u524d\u7ebf\u7a0b\u8282\u70b9\uff0c\u865a\u62df\u8282\u70b9\n        Node node = new Node(Thread.currentThread(), mode);\n        \/\/ Try the fast path of enq; backup to full enq on failure\n        \/\/ 2\u3001\u83b7\u53d6\u5c3e\u8282\u70b9\uff0c\u521d\u59cb\u8fdb\u5165\u8282\u70b9\u662fnull\n        Node pred = tail;\n        \/\/ 3\u3001\u5982\u679c\u5c3e\u8282\u70b9\u4e0d\u4e3anull,\u600e\u5c06\u5f53\u524d\u7ebf\u7a0b\u8282\u70b9\u653e\u5230\u961f\u5217\u5c3e\u90e8\uff0c\u5e76\u8fd4\u56de\u5f53\u524d\u8282\u70b9\n        if (pred != null) {\n            node.prev = pred;\n            if (compareAndSetTail(pred, node)) {\n                pred.next = node;\n                return node;\n            }\n        }\n        \/\/ \u5982\u679c\u5c3e\u8282\u70b9\u4e3anull\uff08\u5176\u5b9e\u662f\u94fe\u8868\u6ca1\u6709\u521d\u59cb\u5316\uff09,\u600e\u8fdb\u5165enq\u65b9\u6cd5\n        enq(node);\n        return node;\n    }\n    \n   \/\/ \u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u8ba4\u4e3a\u662f\u521d\u59cb\u5316\u94fe\u8868\n   private Node enq(final Node node) {\n   \t\t\/\/ 1\u3001\u5165\u961f \uff1a \u4e3a\u4ec0\u4e48\u8981\u7528\u5faa\u73af\u5462\uff1f  \n        for (;;) {\n           \/\/ \u83b7\u53d6\u5c3e\u8282\u70b9\n            Node t = tail;\n           \/\/ 2\u3001\u5c3e\u8282\u70b9\u4e3anull\n            if (t == null) { \/\/ Must initialize\n               \/\/ 2.1 \u521d\u59cb\u8bdd\u5934\u7ed3\u70b9\u548c\u5c3e\u8282\u70b9\n                if (compareAndSetHead(new Node()))\n                    tail = head;\n            } \n            \/\/ 3\u3001\u5c06\u5f53\u524d\u8282\u70b9\u52a0\u5165\u94fe\u8868\u5c3e\u90e8\n            else {\n                node.prev = t;\n                if (compareAndSetTail(t, node)) {\n                    t.next = node;\n                    return t;\n                }\n            }\n        }\n    } <\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48enq\u8981\u7528for(;;)\u5417\uff1f \u548b\u4e00\u770b\u6700\u591a\u53ea\u8981\u5faa\u73af2\u6b21\u554a\uff01 \u7b54\u7591\u6765\u4e86\uff0c\u8fd9\u662f\u5bf9\u4e8e\u5355\u7ebf\u7a0b\u6765\u8bf4\u786e\u5b9e\u662f\u8fd9\u6837\u7684\uff0c\u4f46\u662f\u5bf9\u4e8e\u591a\u7ebf\u7a0b\u6765\u8bf4\uff0c\u6709\u53ef\u80fd\u5728\u7b2c2\u90e8\u5b8c\u6210\u4e4b\u540e\u5c31\u88ab\u522b\u7684\u7ebf\u7a0b\u5148\u6267\u884c\u5165\u94fe\u8868\u4e86\uff0c\u8fd9\u65f6\u5019\u7b2c3\u6b65cas\u4e4b\u540e\u53d1\u73b0\u4e0d\u6210\u529f\u4e86\uff0c\u600e\u4e48\u529e\uff1f\u53ea\u80fd\u518d\u4e00\u6b21\u5faa\u73af\u53bb\u5c1d\u8bd5\u52a0\u5165\u94fe\u8868\uff0c\u76f4\u5230\u6210\u529f\u4e3a\u6b62\u3002<\/strong><\/p>\n<p><strong>aquireQueed\u5206\u6790<\/strong><\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u662f\u4e3a\u4e86\u66f4\u65b0\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u7ebf\u7a0b\u7684\u7b49\u5f85\u72b6\u6001<\/p>\n<pre class=\"code\"> final boolean acquireQueued(final Node node, int arg) {\n         \/\/ \u5931\u8d25\u6807\u8bc6\n        boolean failed = true;\n        try {\n            \/\/ \u4e2d\u65ad\u6807\u8bc6\n            boolean interrupted = false;\n            for (;;) {\n                \/\/ \u83b7\u53d6\u5f53\u524d\u8282\u70b9\u7684\u524d\u4e00\u4e2a\u8282\u70b9\n                final Node p = node.predecessor();\n                \/\/ 1\u3001\u5982\u679c\u524d\u8282\u70b9\u662f\u5934\u7ed3\u70b9\uff0c\u90a3\u4e48\u53bb\u5c1d\u8bd5\u83b7\u53d6\u9501\n                if (p == head &amp;&amp; tryAcquire(arg)) {\n                    \/\/ \u91cd\u7f6e\u5934\u7ed3\u70b9\n                    setHead(node);\n                    p.next = null; \/\/ help GC\n                    \/\/ \u83b7\u5f97\u9501\n                    failed = false;\n                    \/\/ \u8fd4\u56defalse,\u8282\u70b9\u83b7\u5f97\u9501\uff0c\uff0c\uff0c\u7136\u540e\u73b0\u5728\u53ea\u6709\u81ea\u5df1\u4e00\u4e2a\u7ebf\u7a0b\u4e86\u8fd9\u4e2a\u65f6\u5019\u5c31\u4f1a\u81ea\u5df1\u5524\u9192\u81ea\u5df1\n                    \/\/ \u4f7f\u7528\u7684\u662facquire\u4e2d\u7684selfInterrupt(); \n                    return interrupted;\n                }\n                \/\/ 2\u3001\u5982\u679c\u7ebf\u7a0b\u6ca1\u6709\u83b7\u5f97\u9501\uff0c\u4e14\u8282\u70b9waitStatus=0\uff0cshouldParkAfterFailedAcquire\u5e76\u5c06\u8282\u70b9\u7684waitStatus\u8d4b\u503c\u4e3a-1\n                \/\/parkAndCheckInterrupt\u5c06\u7ebf\u7a0bpark\uff0c\u8fdb\u5165\u7b49\u5f85\u6a21\u5f0f\uff0c\n                if (shouldParkAfterFailedAcquire(p, node) &amp;&amp;\n                    parkAndCheckInterrupt())\n                    interrupted = true;\n            }\n        } finally {\n            if (failed)\n                cancelAcquire(node);\n        }\n    }\n\nprivate static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {\n        int ws = pred.waitStatus;\n        if (ws == Node.SIGNAL)\n            \/*\n             * This node has already set status asking a release\n             * to signal it, so it can safely park.\n             *\/\n            return true;\n        if (ws &gt; 0) {\n            \/*\n             * Predecessor was cancelled. Skip over predecessors and\n             * indicate retry.\n             *\/\n            do {\n                node.prev = pred = pred.prev;\n            } while (pred.waitStatus &gt; 0);\n            pred.next = node;\n        } else {\n            \/*\n             * waitStatus must be 0 or PROPAGATE.  Indicate that we\n             * need a signal, but don't park yet.  Caller will need to\n             * retry to make sure it cannot acquire before parking.\n             *\/\n            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);\n        }\n        return false;\n    } <\/pre>\n<h4><strong>Unlock\u6e90\u7801\u5206\u6790<\/strong><\/h4>\n<pre class=\"code\">  public final boolean release(int arg) {\n         \/\/ \u5982\u679c\u6210\u529f\u91ca\u653e\u72ec\u5360\u9501\uff0c\n        if (tryRelease(arg)) {\n            Node h = head;\n            \/\/ \u5982\u679c\u5934\u7ed3\u70b9\u4e0d\u4e3anull\uff0c\u4e14\u540e\u7eed\u6709\u5165\u961f\u7ed3\u70b9\n            if (h != null &amp;&amp; h.waitStatus != 0)\n                \/\/\u91ca\u653e\u5f53\u524d\u7ebf\u7a0b\uff0c\u5e76\u6fc0\u6d3b\u7b49\u5f85\u961f\u91cc\u7684\u7b2c\u4e00\u4e2a\u6709\u6548\u8282\u70b9\n                unparkSuccessor(h);\n            return true;\n        }\n        return false;\n    }\n    \/\/ \u5982\u679c\u91ca\u653e\u9501\u7740\u8fd4\u56detrue,\u5426\u8005\u8fd4\u56defalse\n    \/\/ \u5e76\u4e14\u5c06sate \u8bbe\u7f6e\u4e3a0\n protected final boolean tryRelease(int releases) {\n            int c = getState() - releases;\n            if (Thread.currentThread() != getExclusiveOwnerThread())\n                throw new IllegalMonitorStateException();\n            boolean free = false;\n            if (c == 0) {\n                free = true;\n                setExclusiveOwnerThread(null);\n            }\n            setState(c);\n            return free;\n        }\n\n\n  private void unparkSuccessor(Node node) {\n        \/*\n         * If status is negative (i.e., possibly needing signal) try\n         * to clear in anticipation of signalling.  It is OK if this\n         * fails or if status is changed by waiting thread.\n         *\/\n        int ws = node.waitStatus;\n        if (ws &lt; 0)\n            \/\/ \u91cd\u7f6e\u5934\u7ed3\u70b9\u7684\u72b6\u6001waitStatus\n            compareAndSetWaitStatus(node, ws, 0);\n\n        \/*\n         * Thread to unpark is held in successor, which is normally\n         * just the next node.  But if cancelled or apparently null,\n         * traverse backwards from tail to find the actual\n         * non-cancelled successor.\n         *\/\n         \/\/ \u83b7\u53d6\u5934\u7ed3\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        Node s = node.next;\n        \/\/ s.waitStatus &gt; 0 \u4e3a\u53d6\u6d88\u72b6\u6001 \uff0c\u7ed3\u70b9\u4e3a\u7a7a\u4e14\u88ab\u53d6\u6d88\n        if (s == null || s.waitStatus &gt; 0) {\n            s = null;\n            \/\/ \u83b7\u53d6\u961f\u5217\u91cc\u6ca1\u6709cancel\u7684\u6700\u524d\u9762\u7684\u8282\u70b9\n            for (Node t = tail; t != null &amp;&amp; t != node; t = t.prev)\n                if (t.waitStatus &lt;= 0)\n                    s = t;\n        }\n        \/\/ \u5982\u679c\u8282\u70b9s\u4e0d\u4e3anull\uff0c\u5219\u83b7\u5f97\u9501\n        if (s != null)\n            LockSupport.unpark(s.thread);\n    }<\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/23\/imagef29151f28fd00e7c.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/23\/imagef29151f28fd00e7c.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2><br \/>JAVA\u591a\u7ebf\u7a0b\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u7684\u7ed3\u679c<\/h2>\n<h3>\u5c1d\u8bd5\u4f7f\u7528while\u5faa\u73af\u8f6e\u8be2<\/h3>\n<pre class=\"code\">Thread t = new Thread(() -&gt; {\n    \/\/\u5b50\u7ebf\u7a0b\u8fdb\u884c\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\n    int num = 1000;\n    String s = \"\";\n    for (int i = 0; i &lt; num; i++) {\n        s += \"Java\";\n    }\n    System.out.println(\"t Over\");\n});\n\/\/\u5f00\u59cb\u8ba1\u65f6\nlong start = System.currentTimeMillis();\nSystem.out.println(\"start = \" + start);\nt.start();\nlong end = 0;\nwhile(t.isAlive() == true){\/\/t.getState() != State.TERMINATED\u8fd9\u4e24\u79cd\u5224\u65ad\u65b9\u5f0f\u90fd\u53ef\u4ee5\n    end = System.currentTimeMillis();\n}\nSystem.out.println(\"end = \" + end);\nSystem.out.println(\"end - start = \" + (end - start)); <\/pre>\n<p>\u4f46\u8fd9\u6837\u592a\u6d88\u8017cpu<\/p>\n<h3>\u4f7f\u7528Thread\u7684join\uff08\uff09\u65b9\u6cd5<\/h3>\n<pre class=\"code\">while(t.isAlive() == true){\n    end = System.currentTimeMillis();\n    try {\n        Thread.sleep(10);\n    }catch (InterruptedException e){\n        e.printStackTrace();\n    }\n} <\/pre>\n<h3>sychornized\u7b49\u5f85\u5524\u9192\u673a\u5236<\/h3>\n<pre class=\"code\">Thread t = new Thread(() -&gt; {\n    int num = 1000;\n    String s = \"\";\n    for (int i = 0; i &lt; num; i++) {\n        s += \"Java\";\n    }\n    System.out.println(\"t Over\");\n    synchronized (lock) {\/\/\u83b7\u53d6\u5bf9\u8c61\u9501\n        lock.notify();\/\/\u5b50\u7ebf\u7a0b\u5524\u9192\n    }\n});\n\/\/\u8ba1\u65f6\nlong start = System.currentTimeMillis();\nSystem.out.println(\"start = \" + start);\n\/\/\u542f\u52a8\u5b50\u7ebf\u7a0b\nt.start();\ntry {\n    synchronized (lock) {\/\/\u8fd9\u91cc\u4e5f\u662f\u4e00\u6837\n        lock.wait();\/\/\u4e3b\u7ebf\u7a0b\u7b49\u5f85\n    }\n} catch (InterruptedException e) {\n    e.printStackTrace();\n}\nlong end = System.currentTimeMillis();\nSystem.out.println(\"end = \" + end);\nSystem.out.println(\"end - start = \" + (end - start)); <\/pre>\n<h3>Countdownlatch<\/h3>\n<p>\u7b80\u5355\u6765\u8bf4\uff0cCountDownLatch\u7c7b\u662f\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u521d\u59cb\u7ebf\u7a0b\u6570\uff08\u8bbe\u7f6e\u540e\u4e0d\u80fd\u6539\u53d8\uff09\uff0c\u5728\u5b50\u7ebf\u7a0b\u7ed3\u675f\u65f6\u8c03\u7528countDown()\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7ebf\u7a0b\u6570\u51cf\u4e00\uff0c\u6700\u7ec8\u4e3a0\u7684\u65f6\u5019\uff0c\u8c03\u7528CountDownLatch\u7684\u6210\u5458\u65b9\u6cd5wait()\u7684\u7ebf\u7a0b\u5c31\u4f1a\u53d6\u6d88BLOKED\u963b\u585e\u72b6\u6001\uff0c\u8fdb\u5165RUNNABLE\u4ece\u800c\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<pre class=\"code\">int threadNumber = 1;\nfinal CountDownLatch cdl = new CountDownLatch(threadNumber);\/\/\u53c2\u6570\u4e3a\u7ebf\u7a0b\u4e2a\u6570\n\nThread t = new Thread(() -&gt; {\n    int num = 1000;\n    String s = \"\";\n    for (int i = 0; i &lt; num; i++) {\n        s += \"Java\";\n    }\n    System.out.println(\"t Over\");\n    cdl.countDown();\/\/\u6b64\u65b9\u6cd5\u662fCountDownLatch\u7684\u7ebf\u7a0b\u6570-1\n});\n\nlong start = System.currentTimeMillis();\nSystem.out.println(\"start = \" + start);\nt.start();\n\/\/\u7ebf\u7a0b\u542f\u52a8\u540e\u8c03\u7528countDownLatch\u65b9\u6cd5\ntry {\n    cdl.await();\/\/\u9700\u8981\u6355\u83b7\u5f02\u5e38\uff0c\u5f53\u5176\u4e2d\u7ebf\u7a0b\u6570\u4e3a0\u65f6\u8fd9\u91cc\u624d\u4f1a\u7ee7\u7eed\u8fd0\u884c\n}catch (InterruptedException e){\n    e.printStackTrace();\n}\nlong end = System.currentTimeMillis();\nSystem.out.println(\"end = \" + end);\nSystem.out.println(\"end - start = \" + (end - start)); <\/pre>\n<h3>Future<\/h3>\n<pre class=\"code\">ExecutorService executorService = Executors.newFixedThreadPool(1);\n\nThread t = new Thread(() -&gt; {\n    int num = 1000;\n    String s = \"\";\n    for (int i = 0; i &lt; num; i++) {\n        s += \"Java\";\n    }\n    System.out.println(\"t Over\");\n});\nlong start = System.currentTimeMillis();\nSystem.out.println(\"start = \" + start);\nFuture future = executorService.submit(t);\/\/\u5b50\u7ebf\u7a0b\u542f\u52a8\ntry {\n    future.get();\/\/\u9700\u8981\u6355\u83b7\u4e24\u79cd\u5f02\u5e38\n}catch (InterruptedException e){\n    e.printStackTrace();\n}catch (ExecutionException e){\n    e.printStackTrace();\n}\nlong end = System.currentTimeMillis();\nSystem.out.println(\"end = \" + end);\nSystem.out.println(\"end - start = \" + (end - start));\nexecutorService.shutdown();<\/pre>\n<h3>BlockingQueue<\/h3>\n<p>\u540c\u65f6\uff0c\u5728concurrent\u5305\u4e2d\uff0c\u8fd8\u63d0\u4f9b\u4e86BlockingQueue\uff08\u961f\u5217\uff09\u6765\u64cd\u4f5c\u7ebf\u7a0b\uff0cBlockingQueue\u7684\u4e3b\u8981\u7684\u7528\u6cd5\u662f\u5728\u7ebf\u7a0b\u95f4\u5b89\u5168\u6709\u6548\u7684\u4f20\u9012\u6570\u636e\u3002<\/p>\n<pre class=\"code\">\u00a0BlockingQueue queue = new ArrayBlockingQueue(1);\/\/\u6570\u7ec4\u578b\u961f\u5217\uff0c\u957f\u5ea6\u4e3a1\nThread t = new Thread(() -&gt; {\n    int num = 1000;\n    String s = \"\";\n    for (int i = 0; i &lt; num; i++) {\n        s += \"Java\";\n    }\n    System.out.println(\"t Over\");\n    try {\n        queue.put(\"OK\");\/\/\u5728\u961f\u5217\u4e2d\u52a0\u5165\u6570\u636e\n    } catch (InterruptedException e) {\n        e.printStackTrace();\n    }\n});\nlong start = System.currentTimeMillis();\nSystem.out.println(\"start = \" + start);\nt.start();\ntry {\n    queue.take();\/\/\u4e3b\u7ebf\u7a0b\u5728\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\uff0ctake()\u65b9\u6cd5\u4f1a\u963b\u585e\u961f\u5217\uff0cps\u8fd8\u6709\u4e0d\u4f1a\u963b\u585e\u7684\u65b9\u6cd5\n} catch (InterruptedException e) {\n    e.printStackTrace();\n}\nlong end = System.currentTimeMillis();\nSystem.out.println(\"end = \" + end);\nSystem.out.println(\"end - start = \" + (end - start));<\/pre>\n<h3>CyclicBairrer<\/h3>\n<pre class=\"code\">\u00a0public class Test {\n    public static void main(String[] args) {\n        int N = 4;\n        CyclicBarrier barrier  = new CyclicBarrier(N,new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(\"\u5f53\u524d\u7ebf\u7a0b\"+Thread.currentThread().getName());   \n            }\n        });\n         \n        for(int i=0;i&lt;N;i++)\n            new Writer(barrier).start();\n    }\n    static class Writer extends Thread{\n        private CyclicBarrier cyclicBarrier;\n        public Writer(CyclicBarrier cyclicBarrier) {\n            this.cyclicBarrier = cyclicBarrier;\n        }\n \n        @Override\n        public void run() {\n            System.out.println(\"\u7ebf\u7a0b\"+Thread.currentThread().getName()+\"\u6b63\u5728\u5199\u5165\u6570\u636e...\");\n            try {\n                Thread.sleep(5000);      \/\/\u4ee5\u7761\u7720\u6765\u6a21\u62df\u5199\u5165\u6570\u636e\u64cd\u4f5c\n                System.out.println(\"\u7ebf\u7a0b\"+Thread.currentThread().getName()+\"\u5199\u5165\u6570\u636e\u5b8c\u6bd5\uff0c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u5199\u5165\u5b8c\u6bd5\");\n                cyclicBarrier.await();\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }catch(BrokenBarrierException e){\n                e.printStackTrace();\n            }\n            System.out.println(\"\u6240\u6709\u7ebf\u7a0b\u5199\u5165\u5b8c\u6bd5\uff0c\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u4efb\u52a1...\");\n        }\n    }\n}<\/pre>\n<h3>\u00a0<\/h3>\n<h3>LockSupport<\/h3>\n<pre class=\"code\">\u00a0Thread mainThread = Thread.currentThread();\nThread t = new Thread(() -&gt; {\n    \/\/\u5b50\u7ebf\u7a0b\u8fdb\u884c\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\n    int num = 1000;\n    String s = \"\";\n    for (int i = 0; i &lt; num; i++) {\n        s += \"Java\";\n    }\n    System.out.println(\"t Over\");\n    LockSupport.unpark(mainThread);\n});\n\/\/\u5f00\u59cb\u8ba1\u65f6\nlong start = System.currentTimeMillis();\nSystem.out.println(\"start = \" + start);\nt.start();\nLockSupport.park();\/\/ \u76f8\u5f53\u4e8e\u6682\u505c\u4e3b\u7ebf\u7a0b\nlong end = System.currentTimeMillis();\nSystem.out.println(\"end = \" + end);\nSystem.out.println(\"end - start = \" + (end - start));\n<\/pre>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7f6e\u77e5\u8bc6\uff08LockSupport\uff09 \u7b80\u4ecb LockSupport\u662f\u7528\u6765\u521b\u5efa\u9501\u548c\u5176\u4ed6\u540c\u6b65\u5de5\u5177\u7c7b\u7684\u57fa\u672c\u7ebf\u7a0b\u963b\u585e\u539f\u8bed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":410,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[15,10],"class_list":["post-409","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-aqs","tag-10"],"_links":{"self":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":7,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":420,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/409\/revisions\/420"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/media\/410"}],"wp:attachment":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}