{"id":653,"date":"2020-07-01T14:53:59","date_gmt":"2020-07-01T06:53:59","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=653"},"modified":"2020-07-01T14:53:59","modified_gmt":"2020-07-01T06:53:59","slug":"%e9%94%81%e7%9a%84%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af1-%e5%90%8c%e6%ad%a5%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=653","title":{"rendered":"\u9501\u7684\u5e94\u7528\u573a\u666f1-\u540c\u6b65\u63a5\u53e3"},"content":{"rendered":"\n<p>\u5728\u4ee5\u524d\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u78b0\u5230\u8fd9\u6837\u4e00\u79cd\u573a\u666f\uff1a\u5b9a\u4e49\u4e2a\u63a5\u53e3\uff0c\u4f46\u662f\u63a5\u53e3\u8c03\u7528\u4e86\u5f02\u6b65\u7684\u65b9\u6cd5\u6216\u8005\u7ebf\u7a0b\uff0c\u5bfc\u81f4\u8fd9\u4e2a\u63a5\u53e3\u6ca1\u6cd5\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2acallback\u518d\u5c06\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int interface1(){\n        Util util = new Util();\n        util.call(new CallBack() {\n            @Override\n            public void onResult(int result) {\n\n            }\n        });\n    }\n\n    class Util{\n        void call(CallBack callBack){\n            if(callBack != null){\n                callBack.onResult(0);\n            }\n        }\n    }\n\n    private interface CallBack{\n        void onResult(int result);\n    }<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\u6240\u793a\uff0cinterface1\u8c03\u7528\u4e86Util\u7684call\u65b9\u6cd5\uff0c\u60f3\u5c06result\u76f4\u63a5\u8fd4\u56de\uff0c\u4f46\u662f\u7531\u4e8eUtil\u7684call \u65b9\u6cd5\u662f\u5f02\u6b65\u7684\uff0c\u5bfc\u81f4 interface1 \u6ca1\u6cd5\u76f4\u63a5\u8fd4\u56deresult\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u5728 interface1\u91cc\u5b9a\u4e49\u53d8\u91cfvalue\uff0c\u7136\u540ecallback\u91cc\u8d4b\u503c\uff0c\u6700\u540e\u8fd4\u56de\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int interface1(){\n        final int[] value = {-1};\n        Util util = new Util();\n        util.call(new CallBack() {\n            @Override\n            public void onResult(int result) {\n                value[0] = result;\n            }\n        });\n        return value[0];\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6ca1\u6709\u4efb\u4f55\u7684\u95ee\u9898\u3002\u6211\u5f53\u521d\u4ee5\u4e3a\u4e0a\u9762\u7684\u4ee3\u7801onResult\u56de\u8c03\u53ef\u80fd\u4f1a\u665a\u4e8ereturn value[0];\u6267\u884c\uff0c\u5176\u5b9e\u4e0d\u4f1a\uff0c\u53ea\u8981\u662f\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u6267\u884c\uff0c\u5373\u4f7fcallback\u91cc\u518d\u5d4c\u5957N\u591a\u4e2acallback\u7ed9value\u8d4b\u503c\u3002\u6700\u7ec8\u8fd4\u56de\u7684value\u4ecd\u7136\u662f\u6700\u540e\u4e00\u4e2acallback\u8d4b\u7684\u503c\u3002\uff08\u5199\u4ee3\u7801\u51ed\u611f\u89c9\u771f\u4e0d\u662f\u597d\u4e60\u60ef\uff01\u5bf9\u5f85\u79d1\u5b66\u8981\u4e25\u8c28\uff01\uff09<\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0cutil.call\u5f80\u5f80\u662f\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\uff0c\u5c31\u662f\u7ed9call\u65b9\u6cd5\u52a0\u4e86\u4e2a\u5b50\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> class Util{\n        void call(final CallBack callBack){\n            new Thread(){\n                @Override\n                public void run() {\n                    try {\n                        Thread.sleep(300);\n                    } catch (InterruptedException e) {\n                        e.printStackTrace();\n                    }\n                    if(callBack != null){\n                        callBack.onResult(0);\n                    }\n                }\n            }.start();\n        }\n }<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4e0a\u9762\u7684 interface1 \u4e0d\u52a0\u63a7\u5236\uff0c\u7ed3\u679c\u6709\u53ef\u80fd\u5c31\u8fd4\u56de-1\u3002\u539f\u56e0\u662f\u5b50\u7ebf\u7a0b\u8fd8\u6ca1\u6709callback\uff0c <br> interface1  \u6240\u5728\u7ebf\u7a0b\uff08\u8fd9\u91cc\u6211\u5c31\u79f0\u4e3b\u7ebf\u7a0b\u4e86\uff09\u5c31\u5df2\u7ecfreturn\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u5c31\u5f15\u51fa\u4e86\u672c\u7bc7\u6587\u7ae0\u7684\u4e3b\u9898\uff1a<strong>\u540c\u6b65\u63a5\u53e3\u8fd4\u56de\u5f02\u6b65\u8c03\u7528\u7ed3\u679c\u3002<\/strong>\u4e0b\u9762\u6211\u603b\u7ed3\u51e0\u79cd\u89e3\u51b3\u529e\u6cd5\uff0c\u4f46\u57fa\u672c\u7684\u539f\u7406\u90fd\u662f\uff1a<\/p>\n\n\n\n<p class=\"has-text-color has-luminous-vivid-orange-color\">\u60f3\u529e\u6cd5\u8ba9\u4e3b\u7ebf\u7a0b\u7b49\u5f85\uff0c\u7b49\u5b50\u7ebf\u7a0b\u6709\u7ed3\u679c\u4e86\uff0c\u4e3b\u7ebf\u7a0b\u518d\u8fd4\u56de\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=653\/#1%E4%B8%BB%E7%BA%BF%E7%A8%8B%E9%87%8Cwhile%E5%BE%AA%E7%8E%AF%E7%9B%91%E5%90%AC%E5%88%B0%E7%BB%93%E6%9E%9C%E5%86%8D%E8%BF%94%E5%9B%9E\" title=\"1.\u4e3b\u7ebf\u7a0b\u91ccwhile\u5faa\u73af\u76d1\u542c\u5230\u7ed3\u679c\u518d\u8fd4\u56de\">1.\u4e3b\u7ebf\u7a0b\u91ccwhile\u5faa\u73af\u76d1\u542c\u5230\u7ed3\u679c\u518d\u8fd4\u56de<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=653\/#2%E5%88%A9%E7%94%A8wait%E5%92%8Cnotify\" title=\"2.\u5229\u7528wait\u548cnotify\">2.\u5229\u7528wait\u548cnotify<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=653\/#3%E5%88%A9%E7%94%A8_CountDownLatch%E6%88%96%E8%80%85CyclicBarrier\" title=\"3.\u5229\u7528 CountDownLatch\u6216\u8005CyclicBarrier\">3.\u5229\u7528 CountDownLatch\u6216\u8005CyclicBarrier<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=653\/#3Future\" title=\"3.Future \">3.Future <\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1%E4%B8%BB%E7%BA%BF%E7%A8%8B%E9%87%8Cwhile%E5%BE%AA%E7%8E%AF%E7%9B%91%E5%90%AC%E5%88%B0%E7%BB%93%E6%9E%9C%E5%86%8D%E8%BF%94%E5%9B%9E\"><\/span>1.\u4e3b\u7ebf\u7a0b\u91ccwhile\u5faa\u73af\u76d1\u542c\u5230\u7ed3\u679c\u518d\u8fd4\u56de<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code> public int interface1(){\n        final int[] value = {-1};\n        Util util = new Util();\n        util.call(new CallBack() {\n            @Override\n            public void onResult(int result) {\n                value[0] = result;\n            }\n        });\n        while(value[0] == -1){\n            try {\n                Thread.sleep(100);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n        return value[0];\n}<\/code><\/pre>\n\n\n\n<h2 id=\"mce_15\"><span class=\"ez-toc-section\" id=\"2%E5%88%A9%E7%94%A8wait%E5%92%8Cnotify\"><\/span>2.\u5229\u7528wait\u548cnotify<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public int interface2(){\n        final int[] value = {-1};\n        final Object lock = new Object();\n        Util util = new Util();\n        util.call(new CallBack() {\n            @Override\n            public void onResult(int result) {\n                synchronized (lock){\n                    value[0] = result;\n                    lock.notify();\n                }\n            }\n        });\n        synchronized (lock){\n            try {\n                lock.wait();\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n        return value[0];\n}<\/code><\/pre>\n\n\n\n<h2 id=\"mce_18\"><span class=\"ez-toc-section\" id=\"3%E5%88%A9%E7%94%A8_CountDownLatch%E6%88%96%E8%80%85CyclicBarrier\"><\/span>3.\u5229\u7528 CountDownLatch\u6216\u8005CyclicBarrier<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u89c1<a href=\"https:\/\/www.jianshu.com\/p\/f00aa6f66281\">https:\/\/www.jianshu.com\/p\/f00aa6f66281<\/a><\/p>\n\n\n\n<p>\u8fd9\u4e24\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u591a\u6761\u4ef6\u89e3\u9501\u7684\u573a\u666f\uff0c\u5c31\u662f\u4e3b\u7ebf\u7a0b\u89e3\u9664wait\u53ef\u80fd\u9700\u8981\u591a\u4e2a\u5f02\u6b65\u7ed3\u679c\u5171\u540c\u4f5c\u7528\u3002<\/p>\n\n\n\n<h2 id=\"mce_21\"><span class=\"ez-toc-section\" id=\"3Future\"><\/span>3.Future <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u9002\u7528\u4e8e\u5f02\u6b65\u8c03\u7528\u662f\u53ef\u4ee5\u81ea\u5df1\u638c\u63a7\u4fee\u6539\u7684\u573a\u666f\uff0c\u6bd4\u5982\u4e0a\u9762\u7684Util.call\u91cc\u76f4\u63a5new Thread\u65b9\u5f0f\u5f00\u542f\u7ebf\u7a0b\uff0c\u90a3\u4e48\u53ea\u6709\u901a\u8fc7callback\u7684\u5f62\u5f0f\u6765\u8fd4\u56de\u7ed3\u679c\u3002\u4f46\u662f\u5982\u679c\u4f7f\u7528Future\u6765\u5f00\u542f\u7ebf\u7a0b\uff0c\u90a3\u63a5\u53e3Util.call\u5b8c\u5168\u53ef\u4ee5\u6539\u6210\u540c\u6b65\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int  call(){\n           ExecutorService executor = Executors.newSingleThreadExecutor();\n            Future&lt;Integer> future = executor.submit(new Callable&lt;Integer>() {\n                @Override\n                public Integer call() throws Exception {\n                    try {\n                        Thread.sleep(300);\n                    } catch (InterruptedException e) {\n                        e.printStackTrace();\n                    }\n                    return 0;\n                }\n            });\n            try {\n                return future.get();\n            } catch (ExecutionException e) {\n                e.printStackTrace();\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            return -100;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"653\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u5728\u4ee5\u524d\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u78b0\u5230\u8fd9\u6837\u4e00\u79cd\u573a\u666f\uff1a\u5b9a\u4e49\u4e2a\u63a5\u53e3\uff0c\u4f46\u662f\u63a5\u53e3\u8c03\u7528\u4e86\u5f02\u6b65\u7684\u65b9\u6cd5\u6216\u8005\u7ebf\u7a0b\uff0c\u5bfc\u81f4\u8fd9\u4e2a\u63a5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/653"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":24,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":678,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions\/678"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}