{"id":2418,"date":"2022-07-31T16:57:30","date_gmt":"2022-07-31T08:57:30","guid":{"rendered":"http:\/\/47.101.202.111\/?p=2418"},"modified":"2022-09-27T21:23:29","modified_gmt":"2022-09-27T13:23:29","slug":"aqs-reentrantreadwritelock%e5%ba%94%e7%94%a8%e4%b8%8e%e6%ba%90%e7%a0%81%e8%a7%a3%e8%af%bb","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=2418","title":{"rendered":"AQS &#8211; ReentrantReadWriteLock\u5e94\u7528\u4e0e\u6e90\u7801\u89e3\u8bfb"},"content":{"rendered":"<h3>\u8bfb\u5199\u9501\u6982\u8981<\/h3>\n<p>\u8bfb\u9501\u662f\u5171\u4eab\u9501\uff0c\u5199\u9501\u662f\u6392\u4ed6\u9501<br \/>\n\u516c\u5e73\u9501\u7684\u7b56\u7565\u662f\u5c06\u9501\u5206\u914d\u7ed9\u7b49\u5f85\u65f6\u95f4\u6700\u957f\u7684\u9501\uff0c\u56e0\u6b64\u516c\u5e73\u8bfb\u9501\u3001\u5199\u9501\u662f\u4e0d\u53ef\u91cd\u5165\u9501?<br \/>\n\u5199\u9501\u662f\u6392\u4ed6\u9501\uff0c\u540c\u4e00\u65f6\u95f4\u6bb5\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u5199\u9501\uff0c\u56e0\u6b64\u662f\u4e0d\u53ef\u91cd\u5165\u9501?<br \/>\n\u975e\u516c\u5e73\u8bfb\u9501\u5141\u8bb8\u591a\u4e2a\u8bfb\u7ebf\u7a0b\u540c\u65f6\u83b7\u53d6\u8bfb\u9501\uff0c\u56e0\u6b64\u662f\u53ef\u91cd\u5165\u9501?<br \/>\n\u8bfb\u9501\u4e0d\u80fd\u5347\u7ea7\u4e3a\u5199\u9501\uff0c\u5199\u9501\u53ef\u4ee5\u964d\u7ea7\u4e3a\u8bfb\u9501<br \/>\nCondition\u53ea\u80fd\u548c\u5199\u9501\u642d\u914d\u4f7f\u7528<\/p>\n<h3>\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\u7684\u76f8\u5bb9\u6027<\/h3>\n<table>\n<thead>\n<tr>\n<th align=\"center\"><\/th>\n<th align=\"center\">\u5171\u4eab\u9501<\/th>\n<th align=\"center\">\u6392\u4ed6\u9501<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\"><strong>\u5171\u4eab\u9501<\/strong><\/td>\n<td align=\"center\">\u3007<\/td>\n<td align=\"center\">\u00d7<\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><strong>\u6392\u4ed6\u9501<\/strong><\/td>\n<td align=\"center\">\u00d7<\/td>\n<td align=\"center\">\u00d7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b98\u65b9\u6848\u4f8b\u8bb2\u89e3<\/h3>\n<pre><code class=\"language-java \">class CachedData {\n    Object data;\n    volatile boolean cacheValid;\n    final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();\n\n    void processCachedData() {\n        rwl.readLock().lock();\n        if (!cacheValid) {\n            rwl.readLock().unlock();\n            \/\/ \u8bfb\u9501\n            rwl.writeLock().lock();\n            try {\n                if (!cacheValid) {\n                    data = ...\n                    cacheValid = true;\n                }\n                \/\/ \u5199\u9501\u53ef\u964d\u7ea7\u4e3a\u8bfb\u9501\n                rwl.readLock().lock();\n            } finally {\n                \/\/ \u91ca\u653e\u5199\u9501\uff0c\u4f46\u4e0d\u91ca\u653e\u964d\u7ea7\u540e\u7684\u8bfb\u9501\n                rwl.writeLock().unlock();\n            }\n        }\n\n        try {\n            use(data);\n        } finally {\n            rwl.readLock().unlock();\n        }\n    }\n}\n<\/code><\/pre>\n<pre><code class=\"language-java \">class RWDictionary {\n    private final Map&lt;String, Data&gt; m = new TreeMap&lt;String, Data&gt;();\n    private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();\n    private final Lock r = rwl.readLock();\n    private final Lock w = rwl.writeLock();\n\n    public Data get(String key) {\n        r.lock();\n        try { return m.get(key); }\n        finally { r.unlock(); }\n    }\n    public String[] allKeys() {\n        r.lock();\n        try { return m.keySet().toArray(); }\n        finally { r.unlock(); }\n    }\n    public Data put(String key, Data value) {\n        w.lock();\n        try { return m.put(key, value); }\n        finally { w.unlock(); }\n    }\n    public void clear() {\n        w.lock();\n        try { m.clear(); }\n        finally { w.unlock(); }\n    }\n}\n<\/code><\/pre>\n<h3>\u6e90\u7801\u5206\u6790<\/h3>\n<h3>\u53c2\u8003\u8d44\u6599<\/h3>\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/index.html\">ReentrantReadWriteLock &#8211; Java\u2122 Platform Standard Ed. 8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bfb\u5199\u9501\u6982\u8981 \u8bfb\u9501\u662f\u5171\u4eab\u9501\uff0c\u5199\u9501\u662f\u6392\u4ed6\u9501 \u516c\u5e73\u9501\u7684\u7b56\u7565\u662f\u5c06\u9501\u5206\u914d\u7ed9\u7b49\u5f85\u65f6\u95f4\u6700\u957f\u7684\u9501\uff0c\u56e0\u6b64\u516c\u5e73\u8bfb\u9501\u3001\u5199\u9501\u662f\u4e0d\u53ef\u91cd\u5165 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=2418\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cAQS &#8211; ReentrantReadWriteLock\u5e94\u7528\u4e0e\u6e90\u7801\u89e3\u8bfb\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/2418"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2418"}],"version-history":[{"count":1,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/2418\/revisions"}],"predecessor-version":[{"id":2420,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/2418\/revisions\/2420"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2418"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}