{"id":1919,"date":"2021-03-19T12:26:25","date_gmt":"2021-03-19T04:26:25","guid":{"rendered":"\/?p=1919"},"modified":"2021-03-19T12:26:25","modified_gmt":"2021-03-19T04:26:25","slug":"13-%e5%bc%82%e6%ad%a5%e8%bd%ac%e5%90%8c%e6%ad%a5%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=1919","title":{"rendered":"13.\u5f02\u6b65\u8f6c\u540c\u6b65\u95ee\u9898"},"content":{"rendered":"<p>\u5f02\u6b65\u548c\u540c\u6b65\u4e0d\u5fc5\u8bf4,\u4e00\u4e2a\u9700\u8981\u7b49\u5f85\u7ed3\u679c,\u4e00\u4e2a\u4e0d\u9700\u8981\u7b49\u5f85\u7ed3\u679c<\/p>\n<p>\u5e38\u89c1\u7684\u5f02\u6b65\u7684\u521b\u5efa\u65b9\u5f0f\u6709.\u8c03\u7528\u65b9\u521b\u5efa\u4e00\u4e2a\u5b50\u7ebf\u7a0b,\u5728\u5b50\u7ebf\u7a0b\u4e2d\u6267\u884c\u65b9\u6cd5<\/p>\n<p>\u65b9\u6cd5\u5b9e\u73b0\u7684\u65f6\u5019,\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u6267\u884c\u4e3b\u8981\u903b\u8f91,\u4e3b\u7ebf\u7a0b\u76f4\u63a5return,\u79f0\u4e3a\u5f02\u6b65\u65b9\u6cd5<\/p>\n<p>\u90a3\u4e48\u5bf9\u4e8e\u67d0\u4e9b\u65b9\u6cd5\u9700\u8981\u7b49\u5f85\u5f02\u6b65\u6267\u884c\u5b8c\u6210\u7684\u65b9\u6cd5\u7ed3\u679c,\u8fd9\u5c31\u662f\u5f02\u6b65\u8f6c\u540c\u6b65\u7684\u4e3b\u8981\u9700\u6c42<\/p>\n<p>\u5728\u8c03\u5f02\u6b65\u8c03\u7528\u540e\u963b\u585e\u7ebf\u7a0b,\u5728\u5f02\u6b65\u5b8c\u6210\u5e76\u8fd4\u56de\u7ed3\u679c\u540e,\u5524\u9192\u7ebf\u7a0b,\u7ee7\u7eed\u6267\u884c,\u5c31\u662f\u901a\u77e5\u7b49\u5f85\u673a\u5236\u7684\u52a0\u5f3a\u7248<\/p>\n<p>\/\/ \u521b\u5efa\u9501\u4e0e\u6761\u4ef6\u53d8\u91cf<\/p>\n<p>private final Lock lock<\/p>\n<p>= new ReentrantLock();<\/p>\n<p>private final Condition done<\/p>\n<p>= lock.newCondition();<\/p>\n<p>\/\/ \u8c03\u7528\u65b9\u901a\u8fc7\u8be5\u65b9\u6cd5\u7b49\u5f85\u7ed3\u679c<\/p>\n<p>Object get(int timeout) {<\/p>\n<p>long start = System.nanoTime();<\/p>\n<p>lock.lock();<\/p>\n<p>try {<\/p>\n<p>while (!isDone()) {<\/p>\n<p>done.await(timeout);<\/p>\n<p>long cur = System.nanoTime();<\/p>\n<p>if (isDone() ||<\/p>\n<p>cur &#8211; start &gt; timeout) {<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>} finally {<\/p>\n<p>lock.unlock();<\/p>\n<p>}<\/p>\n<p>if (!isDone()) {<\/p>\n<p>throw new TimeoutException();<\/p>\n<p>}<\/p>\n<p>return returnFromResponse();<\/p>\n<p>}<\/p>\n<p>\/\/ RPC\u7ed3\u679c\u662f\u5426\u5df2\u7ecf\u8fd4\u56de<\/p>\n<p>boolean isDone() {<\/p>\n<p>return response != null;<\/p>\n<p>}<\/p>\n<p>\/\/ RPC\u7ed3\u679c\u8fd4\u56de\u65f6\u8c03\u7528\u8be5\u65b9\u6cd5<\/p>\n<p>private void doReceived(Response res) {<\/p>\n<p>lock.lock();<\/p>\n<p>try {<\/p>\n<p>response = res;<\/p>\n<p>if (done != null) {<\/p>\n<p>done.signal();<\/p>\n<p>}<\/p>\n<p>} finally {<\/p>\n<p>lock.unlock();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u6574\u4f53\u7684\u601d\u8def\u6765\u8bf4,\u5c31\u662f\u901a\u8fc7\u4e0a\u9501,\u5728\u4e0a\u9501\u7684\u540c\u65f6\u5c1d\u8bd5\u8c03\u7528\u83b7\u53d6,\u5982\u679c\u8d85\u65f6\u6216\u8005\u5f97\u5230\u8fd4\u56de,\u8fdb\u884c\u89e3\u9501\u64cd\u4f5c,\u6700\u540e\u5728final\u4e4b\u4e2d\u8fdb\u884c\u89e3\u9501\u7684\u64cd\u4f5c<\/p>\n<p>\u5728\u8fd4\u56de\u65f6\u5019\u8c03\u7528\u4e86doReceived(),\u5728\u5176\u4e2d\u5524\u9192\u4e86\u7ebf\u7a0b\u9501,\u8ba9\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8d70\u4e0b\u53bb<\/p>\n<p>\u6700\u8fd1\u6765\u8bf4,\u5f02\u6b65\u8f6c\u540c\u6b65\u7684\u60c5\u51b5\u8d8a\u6765\u8d8a\u591a\u4e86,\u9700\u8981\u66f4\u52a0\u6ce8\u91cd\u8fd9\u65b9\u9762<\/p>\n<p>\u4f8b\u5982,\u5bf9\u4e8e\u4e00\u4e2a\u4e91\u4e3b\u673a\u7684\u5e38\u89c1,\u5728\u521b\u5efa\u4e91\u4e3b\u673a\u7684\u65f6\u5019,\u672c\u8eab\u5c31\u662f\u5f02\u6b65,\u53ea\u662f\u53d1\u8d77\u4e86\u4e00\u4e2a\u521b\u5efa\u7684\u8bf7\u6c42<\/p>\n<p>\u4f46\u662f\u5e76\u4e0d\u5f97\u77e5\u662f\u5426\u5df2\u7ecf\u521b\u5efa\u6210\u529f\u4e86,\u8fd9\u5c31\u9700\u8981\u5229\u7528\u53e6\u4e00\u4e2aAPI\u53bb\u8f6e\u8be2\u4e91\u4e3b\u673a\u7684\u72b6\u6001<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u6b65\u548c\u540c\u6b65\u4e0d\u5fc5\u8bf4,\u4e00\u4e2a\u9700\u8981\u7b49\u5f85 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/1919"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1919"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/1919\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1919"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}