{"id":83,"date":"2023-02-13T21:57:28","date_gmt":"2023-02-13T13:57:28","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=83"},"modified":"2023-02-13T21:57:50","modified_gmt":"2023-02-13T13:57:50","slug":"java-multi-threag-programming-lock","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/13\/java-multi-threag-programming-lock\/","title":{"rendered":"Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u516d\uff08\u4f7f\u7528Lock\uff09"},"content":{"rendered":"<h2>\u53ef\u91cd\u5165\u9501ReentrantLock<\/h2>\n<h3>\u4f7f\u7528ReentrantLock\u65b9\u6cd5<\/h3>\n<ul>\n<li><code>lock()<\/code>\uff1a\u9501\u5b9a<\/li>\n<li><code>unlock()<\/code>\uff1a\u89e3\u9664\u9501\u5b9a<\/li>\n<li><code>int getHoldCount()<\/code>\uff1a\u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b\u4fdd\u6301\u6b64\u9501\u5b9a\u7684\u4e2a\u6570\uff0c\u4e5f\u5c31\u662f\u8c03\u7528<code>lock()<\/code>\u65b9\u6cd5\u7684\u6b21\u6570<\/li>\n<li><code>int getQueueLength()<\/code>\uff1a\u8fd4\u56de\u6b63\u5728\u7b49\u5f85\u83b7\u53d6\u6b64\u9501\u5b9a\u7684\u7ebf\u7a0b\u4f30\u8ba1\u6570\uff0c\u4f8b\u5982\uff1a8\u4e2a\u7ebf\u7a0b\uff0c3\u4e2a\u7ebf\u7a0b\u8c03\u7528\u4e86<code>await()<\/code>\u65b9\u6cd5\uff0c\u90a3\u4e48\u5728\u8c03\u7528\u6b64\u65b9\u6cd5\u540e\u8fd4\u56de\u7684\u503c\u662f5\uff0c\u8bf4\u660e\u67095\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5728\u7b49\u5f85lock\u7684\u91ca\u653e<\/li>\n<li><code>int getWaitQueueLength()<\/code>\uff1a\u987e\u540d\u601d\u4e49\uff0c\u5176\u4f5c\u7528\u662f\u8fd4\u56de\u7b49\u5f85\u4e0e\u6b64\u9501\u5b9a\u76f8\u5173\u7684\u7ed9\u5b9a\u6761\u4ef6Condition\u7684\u7ebf\u7a0b\u4f30\u8ba1\u6570\u3002\u4f8b\u5982\uff1a8\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u8c03\u7528\u4e86\u540c\u4e00\u4e2aCondition\u7684<code>await()<\/code>\u65b9\u6cd5\uff0c\u5219\u8c03\u7528\u6b64\u65b9\u6cd5\u8fd4\u56de\u7684\u503c\u662f8<\/li>\n<li><code>boolean hasQueuedThread()<\/code>\uff1a\u67e5\u8be2\u6307\u5b9a\u7ebf\u7a0b\u662f\u5426\u6b63\u5728\u7b49\u5f85\u83b7\u53d6\u6b64\u9501\u5b9a<code>ReentrantLock.hasQueuedThread(thread)<\/code><\/li>\n<li><code>boolean hasWaiters()<\/code>\uff1a\u67e5\u8be2\u662f\u5426\u6709\u7ebf\u7a0b\u6b63\u5728\u7b49\u5f85\u4e0e\u6b64\u9501\u5b9a\u6709\u5173\u7684Condition\u6761\u4ef6<\/li>\n<li><code>boolean isFair()<\/code>\uff1a\u5224\u65ad\u662f\u5426\u662f\u516c\u5e73\u9501<\/li>\n<li><code>boolean isHeldByCurrentThread()<\/code>\uff1a\u67e5\u8be2\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u4fdd\u6301\u6b64\u9501\u5b9a<\/li>\n<li><code>boolean isLocked()<\/code>\uff1a\u67e5\u8be2\u6b64\u9501\u5b9a\u662f\u5426\u7531\u4efb\u610f\u7ebf\u7a0b\u4fdd\u6301<\/li>\n<li><code>void lockInterruptibly()<\/code>\uff1a\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u672a\u88ab\u4e2d\u65ad\uff0c\u5219\u83b7\u53d6\u9501\u5b9a\uff0c\u5982\u679c\u5df2\u7ecf\u88ab\u4e2d\u65ad\u5219\u51fa\u73b0\u5f02\u5e38<\/li>\n<li><code>boolean tryLock()<\/code>\uff1a\u4ec5\u5728\u8c03\u7528\u65f6\u9501\u5b9a\u672a\u88ab\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4fdd\u6301\u7684\u60c5\u51b5\u4e0b\uff0c\u624d\u83b7\u53d6\u8be5\u9501\u5b9a<\/li>\n<li><code>boolean tryLock(long timeout, TimeUnit unit)<\/code>\uff1a\u5982\u679c\u9501\u5b9a\u5728\u7ed9\u5b9a\u7b49\u5f85\u65f6\u95f4\u5185\u6ca1\u6709\u88ab\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4fdd\u6301\uff0c\u4e14\u5f53\u524d\u7ebf\u7a0b\u672a\u88ab\u4e2d\u65ad\uff0c\u5219\u83b7\u53d6\u8be5\u9501\u5b9a<\/li>\n<\/ul>\n<h3>\u4f7f\u7528ReentrantLock\u5b9e\u73b0\u540c\u6b65\u6548\u679c<\/h3>\n<pre><code class=\"language-java\">public class MyServer {\n    private ReentrantLock lock = new ReentrantLock();\n    public void printReentrantLock() {\n        try {\n            lock.lock();\/\/\u83b7\u53d6\u9501\n            for (int i = 0; i &lt; 5; i++) {\n                System.out.println(&quot;ThreadName=&quot; + Thread.currentThread().getName() + &quot;,i=&quot; + i);\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            lock.unlock();\/\/\u91ca\u653e\u9501\n        }\n    }\n    public static void main(String[] args) {\n        final MyServer myServer = new MyServer();\n        for (int i = 0; i &lt; 5; i++) {\n            Thread thread = new Thread(new Runnable() {\n                public void run() {\n                    myServer.printReentrantLock();\n                }\n            });\n            thread.setName(String.valueOf(i));\n            thread.start();\n        }\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>ThreadName=0,i=0\nThreadName=0,i=1\nThreadName=0,i=2\nThreadName=0,i=3\nThreadName=0,i=4\nThreadName=1,i=0\nThreadName=1,i=1\nThreadName=1,i=2\nThreadName=1,i=3\nThreadName=1,i=4\nThreadName=2,i=0\nThreadName=2,i=1\nThreadName=2,i=2\nThreadName=2,i=3\nThreadName=2,i=4\nThreadName=3,i=0\nThreadName=3,i=1\nThreadName=3,i=2\nThreadName=3,i=3\nThreadName=3,i=4\nThreadName=4,i=0\nThreadName=4,i=1\nThreadName=4,i=2\nThreadName=4,i=3\nThreadName=4,i=4<\/code><\/pre>\n<p>\u7531\u7ed3\u679c\u5206\u6790\u77e5\uff1a\u5f53\u524d\u7ebf\u7a0b\u83b7\u53d6\u9501\u5bf9\u8c61\u540e\u5176\u4ed6\u7ebf\u7a0b\u5448\u963b\u585e\u72b6\u6001\uff0c\u76f4\u5230\u5f53\u524d\u7ebf\u7a0b\u6253\u5370\u5b8c\u6bd5\u91ca\u653e\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u624d\u80fd\u83b7\u53d6\u5230\u9501\u8fdb\u884c\u6253\u5370\u3002<\/p>\n<p>\u8fd9\u4e2a\u4e3b\u8981\u8fdb\u884c\u5355\u4e2a\u65b9\u6cd5\u540c\u4e00\u4e2a\u9501\u591a\u7ebf\u7a0b\u8c03\u7528\u7684\u540c\u6b65\u6548\u679c\uff0c\u4e0b\u9762\u6765\u770b\u4e00\u4e0b\u591a\u4e2a\u7ebf\u7a0b\u4ea4\u53c9\u8c03\u7528\u4e0d\u540c\u65b9\u6cd5\u540c\u4e00\u4e2a\u9501\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyServer {\n\n    private ReentrantLock lock = new ReentrantLock();\n\n    public void methodA() {\n        try {\n            lock.lock();\/\/\u83b7\u53d6\u9501\n            System.out.println(&quot;method A begin ThreadName=&quot; + Thread.currentThread().getName() + &quot; time=&quot; + System.currentTimeMillis());\n            methodB();\/\/\u8c03\u7528methodB\n            Thread.sleep(5000);\n            System.out.println(&quot;method A end   ThreadName=&quot; + Thread.currentThread().getName() + &quot; time=&quot; + System.currentTimeMillis());\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public void methodB() {\n        try {\n            lock.lock();\/\/\u83b7\u53d6\u9501\n            System.out.println(&quot;method B begin ThreadName=&quot;+Thread.currentThread().getName()+&quot; time=&quot;+System.currentTimeMillis());\n            Thread.sleep(5000);\n            System.out.println(&quot;method B end   ThreadName=&quot;+Thread.currentThread().getName()+&quot; time=&quot;+System.currentTimeMillis());\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        } finally {\n            lock.unlock();\n        }\n    }\n}\n\npublic static void main(String[] args) {\n    String[] names = {&quot;A&quot;, &quot;AA&quot;, &quot;B&quot;, &quot;BB&quot;};\n    final MyServer myServer = new MyServer();\n    Thread[] threads = new Thread[4];\n    for (int i = 0; i &lt; 2; i++) {\n        threads[i] = new Thread(new Runnable() {\n            public void run() {\n                myServer.methodA();\n            }\n        });\n    }\n    for (int i = 2; i &lt; 4; i++) {\n        threads[i] = new Thread(new Runnable() {\n            public void run() {\n                myServer.methodB();\n            }\n        });\n    }\n    for (int i = 0; i &lt; 4; i++) {\n        threads[i].setName(names[i]);\n        threads[i].start();\n    }\n}<\/code><\/pre>\n<pre><code>method A begin ThreadName=A time=1551406905681\nmethod B begin ThreadName=A time=1551406905681\nmethod B end   ThreadName=A time=1551406910685\nmethod A end   ThreadName=A time=1551406915689\nmethod A begin ThreadName=AA time=1551406915689\nmethod B begin ThreadName=AA time=1551406915690\nmethod B end   ThreadName=AA time=1551406920691\nmethod A end   ThreadName=AA time=1551406925691\nmethod B begin ThreadName=B time=1551406925691\nmethod B end   ThreadName=B time=1551406930693\nmethod B begin ThreadName=BB time=1551406930693\nmethod B end   ThreadName=BB time=1551406935698<\/code><\/pre>\n<p>\u5206\u6790\u7ed3\u679c\uff1a\u5728\u7ebf\u7a0bA\u83b7\u53d6\u9501\u65f6\u5176\u4ed6\u7ebf\u7a0b\u5448\u963b\u585e\u72b6\u6001\u76f4\u81f3\u7ebf\u7a0bA\u6267\u884c\u5b8c\u6bd5\u4e14\u91ca\u653e\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u624d\u53ef\u4ee5\u83b7\u53d6\u5230\u9501\uff0c\u540c\u65f6\u5728\u7ebf\u7a0bA\u83b7\u53d6\u9501\u6267\u884c\u8fc7\u7a0b\u4e2d\u53c8\u6210\u529f\u8c03\u7528\u4e86methodB\uff0c\u8fd9\u4e9b\u8bf4\u660e<code>reentrantlock<\/code>\u548c<code>synchronized<\/code>\u5173\u952e\u5b57\u4e00\u6837\u7ebf\u7a0b\u4e4b\u95f4\u662f\u540c\u6b65\u987a\u5e8f\u6267\u884c\u7684\uff0c\u4e5f\u90fd\u5177\u6709\u9501\u91cd\u5165\u7279\u6027\u3002<\/p>\n<h2>\u4f7f\u7528Condition\u5b9e\u73b0\u7b49\u5f85\/\u901a\u77e5<\/h2>\n<p>\u5173\u952e\u5b57<code>synchronized<\/code>\u4e0e<code>wait()<\/code>\u548c<code>notify()\/notifyAll()<\/code>\u65b9\u6cd5\u76f8\u7ed3\u5408\u53ef\u4ee5\u5b9e\u73b0\u7b49\u5f85\/\u901a\u77e5\u6a21\u5f0f\uff0c\u7c7b<code>ReentrantLock<\/code>\u4e5f\u53ef\u4ee5\u8bd5\u60f3\u540c\u6837\u7684\u529f\u80fd\uff0c\u4f46\u9700\u8981\u501f\u52a9\u4e8e<code>Condition<\/code>\u5bf9\u8c61\u3002\u76f8\u5bf9\u4e8e<code>notify<\/code>\u548c<code>notifyAll<\/code>\uff0c<code>Condition<\/code>\u5177\u6709\u8ddf\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5728\u4f7f\u7528<code>notify<\/code>\u548c<code>notifyAll<\/code>\u65f6\uff0c\u88ab\u901a\u77e5\u7684\u7ebf\u7a0b\u662f\u7531JVM\u968f\u673a\u9009\u62e9\u7684\uff0c\u800c<code>ReentrantLock<\/code>\u7ed3\u5408<code>Condition<\/code>\u7c7b\u662f\u53ef\u4ee5\u201c\u9009\u62e9\u6027\u901a\u77e5\u7684\u201d\u3002<\/p>\n<h3>Condition\u4e2d\u901a\u77e5\/\u7b49\u5f85\u65b9\u6cd5<\/h3>\n<ul>\n<li><code>await()<\/code>\uff1a\u76f8\u5f53\u4e8eObject\u7c7b\u4e2d\u7684<code>wait()<\/code>\u65b9\u6cd5<\/li>\n<li><code>await(long time, TimeUtil unit)<\/code>\uff1a\u76f8\u5f53\u4e8eObject\u7c7b\u4e2d\u7684<code>wait(long timeout)<\/code>\u65b9\u6cd5<\/li>\n<li><code>signal()<\/code>\uff1a\u76f8\u5f53\u4e8eObject\u7c7b\u4e2d\u7684<code>notify()<\/code>\u65b9\u6cd5<\/li>\n<li><code>signalAll()<\/code>\uff1a\u76f8\u5f53\u4e8eObject\u7c7b\u4e2d\u7684<code>notifyAll()<\/code>\u65b9\u6cd5<\/li>\n<\/ul>\n<h3>\u7b80\u5355\u4f7f\u7528<\/h3>\n<p>\u6ce8\u610f\uff1a\u8c03\u7528<code>condition.await()<\/code>\u4e4b\u524d\u9700\u8981\u6267\u884c<code>lock.lock()<\/code>\u6765\u83b7\u53d6\u540c\u6b65\u76d1\u89c6\u5668\u3002<\/p>\n<pre><code class=\"language-java\">public class MyServer {\n    private ReentrantLock lock = new ReentrantLock();\n    private Condition condition = lock.newCondition();\n\n    public void await() {\n        try {\n            lock.lock();\n            System.out.println(&quot;await  \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n            condition.await();\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public void signal() {\n        try {\n            lock.lock();\n            System.out.println(&quot;signal \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n            condition.signal();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public static void main(String[] args) throws InterruptedException {\n        final MyServer myServer = new MyServer();\n        new Thread(new Runnable() {\n            public void run() {\n                myServer.await();\n            }\n        }).start();\n        Thread.sleep(3000);\n        myServer.signal();\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>await  \u65f6\u95f4\u4e3a 1551420692029\nsignal \u65f6\u95f4\u4e3a 1551420695031<\/code><\/pre>\n<h3>\u4f7f\u7528\u591a\u4e2aCondition\u5b9e\u73b0\u201c\u9009\u62e9\u6027\u201d\u901a\u77e5\u7ebf\u7a0b<\/h3>\n<pre><code class=\"language-java\">public class MyServer {\n    private ReentrantLock lock = new ReentrantLock();\n    private Condition conditionA = lock.newCondition();\n    private Condition conditionB = lock.newCondition();\n\n    @SuppressWarnings(&quot;all&quot;)\n    public void awaitA() {\n        try {\n            lock.lock();\n            System.out.println(&quot;begin await A \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n            conditionA.await();\n            System.out.println(&quot;end   await A \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    @SuppressWarnings(&quot;all&quot;)\n    public void awaitB() {\n        try {\n            lock.lock();\n            System.out.println(&quot;begin await B \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n            conditionB.await();\n            System.out.println(&quot;end   await B \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    @SuppressWarnings(&quot;all&quot;)\n    public void signalAll_A() {\n        try {\n            lock.lock();\n            System.out.println(&quot;  signalall A \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n            conditionA.signalAll();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    @SuppressWarnings(&quot;all&quot;)\n    public void signalAll_B() {\n        try {\n            lock.lock();\n            System.out.println(&quot;  signalall B \u65f6\u95f4\u4e3a &quot; + System.currentTimeMillis());\n            conditionB.signalAll();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public static void main(String[] args) throws InterruptedException {\n        final MyServer myServer = new MyServer();\n        Thread threadA = new Thread(new Runnable() {\n            public void run() {\n                myServer.awaitA();\n            }\n        });\n        threadA.setName(&quot;A&quot;);\n        threadA.start();\n\n        Thread threadB = new Thread(new Runnable() {\n            public void run() {\n                myServer.awaitB();\n            }\n        });\n        threadB.setName(&quot;B&quot;);\n        threadB.start();\n\n        Thread.sleep(3000);\n        myServer.signalAll_B();\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2aCondition\uff0c\u5206\u522b\u7528\u4e8e\u4e24\u4e2a\u7ebf\u7a0b\u7684\u7b49\u5f85\u548c\u5524\u9192\uff0c\u800c\u6211\u4eec\u53ea\u5524\u9192\u7ebf\u7a0bB\u800c\u7ebf\u7a0bA\u5219\u7ee7\u7eed\u7b49\u5f85\u3002\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>begin await A \u65f6\u95f4\u4e3a 1551424032113\nbegin await B \u65f6\u95f4\u4e3a 1551424032116\n  signalall B \u65f6\u95f4\u4e3a 1551424035121\n  end await B \u65f6\u95f4\u4e3a 1551424035122<\/code><\/pre>\n<h2>\u516c\u5e73\u9501\u4e0e\u975e\u516c\u5e73\u9501<\/h2>\n<p>\u516c\u5e73\u9501\u5c31\u662f\u7ebf\u7a0b\u83b7\u53d6\u9501\u7684\u987a\u5e8f\u662f\u6309\u7167\u7ebf\u7a0b\u52a0\u9501\u7684\u987a\u5e8f\u6765\u5206\u914d\u7684\uff0c\u5373\u5148\u6765\u5148\u5f97\u7684FIFO\u5148\u8fdb\u5148\u51fa\u7684\u987a\u5e8f\u3002\u975e\u516c\u5e73\u9501\u5c31\u662f\u968f\u673a\u83b7\u53d6\u9501\u7684\uff0c\u5c5e\u4e8e\u4e00\u79cd\u67aa\u5360\u673a\u5236\u3002\u5148\u6765\u4e0d\u4e00\u5b9a\u5148\u5f97\u5230\uff0c\u8c01\u62a2\u5230\u5c5e\u4e8e\u8c01\uff0c\u6240\u4ee5\u662f\u4e0d\u516c\u5e73\u7684\u3002<\/p>\n<p>\u901a\u8fc7\u6784\u9020\u65b9\u6cd5<code>ReetrantLock(boolean isFair)<\/code>\u6765\u521b\u5efa\u5bf9\u5e94\u516c\u5e73\u9501\u6216\u4e0d\u516c\u5e73\u9501\u3002\u65e0\u53c2\u9ed8\u8ba4\u4e3a\u975e\u516c\u5e73\u9501\u3002<\/p>\n<h2>\u4f7f\u7528ReentrantReadWriteLock\u7c7b<\/h2>\n<p>\u4f7f\u7528<code>ReetrantLock<\/code>\u5177\u6709\u5b8c\u5168\u4e92\u65a5\u6392\u4ed6\u7684\u6548\u679c\uff0c\u5373\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u591f\u6267\u884c<code>Reentrantlock.lock()<\/code>\u65b9\u6cd5\u540e\u7684\u4ee3\u7801\u3002\u867d\u7136\u8fd9\u6837\u80fd\u4fdd\u6301\u5b9e\u4f8b\u53d8\u91cf\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\uff0c\u4f46\u662f\u6548\u7387\u786e\u5b9e\u975e\u5e38\u4f4e\u7684\u3002\u7136\u800c\u4f7f\u7528\u8bfb\u5199\u9501<code>ReentrantReadWriteLock<\/code>\u7c7b\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8fd0\u884c\u6548\u7387\uff0c\u5728\u4e0d\u9700\u8981\u64cd\u4f5c\u5b9e\u4f8b\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bfb\u5199\u9501<code>ReentrantReadWriteLock<\/code>\u6765\u63d0\u9ad8\u4efb\u52a1\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<p>\u8bfb\u5199\u9501<code>ReentrantReadWriteLock<\/code>\u6709\u4e24\u4e2a\u9501\uff0c\u4e00\u4e2a\u662f\u8bfb\u64cd\u4f5c\u7684\u9501\u5373\u5171\u4eab\u9501\uff0c\u4e00\u4e2a\u662f\u5199\u64cd\u4f5c\u9501\u5373\u6392\u5b83\u9501\u3002\u5728\u591a\u4e2a\u8bfb\u9501\u4e4b\u95f4\u4e0d\u4e92\u65a5\uff0c\u8bfb\u9501\u548c\u5199\u9501\u4e92\u65a5\uff0c\u5199\u9501\u548c\u5199\u9501\u4e92\u65a5\u3002\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u7ebf\u7a0b\u8fdb\u884c\u5199\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u884c\u8bfb\u64cd\u4f5c\uff1b\u800c\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u5199\u64cd\u4f5c\u65f6\uff0c\u5176\u4ed6\u7ebf\u7a0b\u7684\u8bfb\u64cd\u4f5c\u6216\u5199\u64cd\u4f5c\u90fd\u53ea\u80fd\u7b49\u5f53\u524d\u7684\u5199\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<h3>\u8bfb\u5199\u6216\u5199\u8bfb\u4e92\u65a5<\/h3>\n<pre><code class=\"language-java\">public class MyServer {\n    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();\n\n    public void read() {\n        try {\n            lock.readLock().lock();\n            System.out.println(&quot;\u83b7\u5f97\u8bfb\u9501\uff1a&quot;+Thread.currentThread().getName()+&quot; &quot;+System.currentTimeMillis());\n            Thread.sleep(10000);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        } finally {\n            lock.readLock().unlock();\n        }\n    }\n\n    public void write() {\n        try {\n            lock.writeLock().lock();\n            System.out.println(&quot;\u83b7\u5f97\u5199\u9501\uff1a&quot;+Thread.currentThread().getName()+&quot; &quot;+System.currentTimeMillis());\n            Thread.sleep(10000);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        } finally {\n            lock.writeLock().unlock();\n        }\n    }\n\n    public static void main(String[] args) throws InterruptedException {\n        final MyServer myServer = new MyServer();\n        Thread threadA = new Thread(new Runnable() {\n            public void run() {\n                myServer.read();\n            }\n        });\n        threadA.setName(&quot;A&quot;);\n        threadA.start();\n\n        Thread threadB = new Thread(new Runnable() {\n            public void run() {\n                myServer.write();\n            }\n        });\n        threadB.setName(&quot;B&quot;);\n        threadB.start();\n\n        Thread threadC = new Thread(new Runnable() {\n            public void run() {\n                myServer.read();\n            }\n        });\n        threadC.setName(&quot;C&quot;);\n        threadC.start();\n    }\n}<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\u83b7\u5f97\u8bfb\u9501\uff1aA 1551509003778\n\u83b7\u5f97\u8bfb\u9501\uff1aA 1551509013785\n\u83b7\u5f97\u8bfb\u9501\uff1aA 1551509023789<\/code><\/pre>\n<blockquote>\n<p>\u7531\u7ed3\u679c\u53ef\u77e5\uff0c\u8bfb\u5199\u548c\u5199\u8bfb\u662f\u4e92\u65a5\u7684<\/p>\n<\/blockquote>\n<h3>\u8bfb\u8bfb\u5171\u4eab<\/h3>\n<p>\u66f4\u6539\u4e0a\u9762\u7684main\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">public static void main(String[] args) throws InterruptedException {\n    final MyServer myServer = new MyServer();\n\n    Thread threadA = new Thread(new Runnable() {\n        public void run() {\n            myServer.read();\n        }\n    });\n    threadA.setName(&quot;A&quot;);\n    threadA.start();\n\n    Thread threadB = new Thread(new Runnable() {\n        public void run() {\n            myServer.read();\n        }\n    });\n    threadB.setName(&quot;B&quot;);\n    threadB.start();\n}<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\u83b7\u5f97\u8bfb\u9501\uff1aA 1551509377678\n\u83b7\u5f97\u8bfb\u9501\uff1aB 1551509377678<\/code><\/pre>\n<blockquote>\n<p>\u7531\u7ed3\u679c\u53ef\u77e5\uff1a\u8bfb\u548c\u8bfb\u5171\u4eab<\/p>\n<\/blockquote>\n<h3>\u5199\u5199\u4e92\u65a5<\/h3>\n<p>\u66f4\u6539\u4e0a\u9762\u7684main\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">public static void main(String[] args) throws InterruptedException {\n    final MyServer myServer = new MyServer();\n\n    Thread threadA = new Thread(new Runnable() {\n        public void run() {\n            myServer.write();\n        }\n    });\n    threadA.setName(&quot;A&quot;);\n    threadA.start();\n\n    Thread threadB = new Thread(new Runnable() {\n        public void run() {\n            myServer.write();\n        }\n    });\n    threadB.setName(&quot;B&quot;);\n    threadB.start();\n}<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\u83b7\u5f97\u5199\u9501\uff1aA 1551509613785\n\u83b7\u5f97\u5199\u9501\uff1aB 1551509623788<\/code><\/pre>\n<blockquote>\n<p>\u7531\u7ed3\u679c\u77e5\uff1a\u5199\u5199\u4e92\u65a5<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u91cd\u5165\u9501ReentrantLock \u4f7f\u7528ReentrantLock\u65b9\u6cd5 lock()\uff1a\u9501\u5b9a unlock()\uff1a [&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":[52],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-lock"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/83","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=83"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}