{"id":31640,"date":"2022-08-14T10:40:37","date_gmt":"2023-03-17T12:22:09","guid":{"rendered":"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/"},"modified":"2024-01-15T06:50:17","modified_gmt":"2024-01-14T22:50:17","slug":"java%e7%9a%84future%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/","title":{"rendered":"Java\u7684Future\u63a5\u53e3"},"content":{"rendered":"<h1>Java API\u4ee3\u7801<\/h1>\n<pre class=\"post-pre\"><code>\r\n<span class=\"kd\">public<\/span> <span class=\"kd\">interface<\/span> <span class=\"nc\">Future<\/span><span class=\"o\">&lt;<\/span><span class=\"no\">V<\/span><span class=\"o\">&gt;<\/span> <span class=\"o\">{<\/span>\r\n\r\n    <span class=\"cm\">\/**\r\n     * Attempts to cancel execution of this task.  This attempt will\r\n     * fail if the task has already completed, has already been cancelled,\r\n     * or could not be cancelled for some other reason. If successful,\r\n     * and this task has not started when {@code cancel} is called,\r\n     * this task should never run.  If the task has already started,\r\n     * then the {@code mayInterruptIfRunning} parameter determines\r\n     * whether the thread executing this task should be interrupted in\r\n     * an attempt to stop the task.\r\n     *\r\n     * &lt;p&gt;After this method returns, subsequent calls to {@link #isDone} will\r\n     * always return {@code true}.  Subsequent calls to {@link #isCancelled}\r\n     * will always return {@code true} if this method returned {@code true}.\r\n     *\r\n     * @param mayInterruptIfRunning {@code true} if the thread executing this\r\n     * task should be interrupted; otherwise, in-progress tasks are allowed\r\n     * to complete\r\n     * @return {@code false} if the task could not be cancelled,\r\n     * typically because it has already completed normally;\r\n     * {@code true} otherwise\r\n     *\/<\/span>\r\n    <span class=\"kt\">boolean<\/span> <span class=\"nf\">cancel<\/span><span class=\"o\">(<\/span><span class=\"kt\">boolean<\/span> <span class=\"n\">mayInterruptIfRunning<\/span><span class=\"o\">);<\/span>\r\n\r\n    <span class=\"cm\">\/**\r\n     * Returns {@code true} if this task was cancelled before it completed\r\n     * normally.\r\n     *\r\n     * @return {@code true} if this task was cancelled before it completed\r\n     *\/<\/span>\r\n    <span class=\"kt\">boolean<\/span> <span class=\"nf\">isCancelled<\/span><span class=\"o\">();<\/span>\r\n\r\n    <span class=\"cm\">\/**\r\n     * Returns {@code true} if this task completed.\r\n     *\r\n     * Completion may be due to normal termination, an exception, or\r\n     * cancellation -- in all of these cases, this method will return\r\n     * {@code true}.\r\n     *\r\n     * @return {@code true} if this task completed\r\n     *\/<\/span>\r\n    <span class=\"kt\">boolean<\/span> <span class=\"nf\">isDone<\/span><span class=\"o\">();<\/span>\r\n\r\n    <span class=\"cm\">\/**\r\n     * Waits if necessary for the computation to complete, and then\r\n     * retrieves its result.\r\n     *\r\n     * @return the computed result\r\n     * @throws CancellationException if the computation was cancelled\r\n     * @throws ExecutionException if the computation threw an\r\n     * exception\r\n     * @throws InterruptedException if the current thread was interrupted\r\n     * while waiting\r\n     *\/<\/span>\r\n    <span class=\"no\">V<\/span> <span class=\"nf\">get<\/span><span class=\"o\">()<\/span> <span class=\"kd\">throws<\/span> <span class=\"nc\">InterruptedException<\/span><span class=\"o\">,<\/span> <span class=\"nc\">ExecutionException<\/span><span class=\"o\">;<\/span>\r\n\r\n    <span class=\"cm\">\/**\r\n     * Waits if necessary for at most the given time for the computation\r\n     * to complete, and then retrieves its result, if available.\r\n     *\r\n     * @param timeout the maximum time to wait\r\n     * @param unit the time unit of the timeout argument\r\n     * @return the computed result\r\n     * @throws CancellationException if the computation was cancelled\r\n     * @throws ExecutionException if the computation threw an\r\n     * exception\r\n     * @throws InterruptedException if the current thread was interrupted\r\n     * while waiting\r\n     * @throws TimeoutException if the wait timed out\r\n     *\/<\/span>\r\n    <span class=\"no\">V<\/span> <span class=\"nf\">get<\/span><span class=\"o\">(<\/span><span class=\"kt\">long<\/span> <span class=\"n\">timeout<\/span><span class=\"o\">,<\/span> <span class=\"nc\">TimeUnit<\/span> <span class=\"n\">unit<\/span><span class=\"o\">)<\/span>\r\n        <span class=\"kd\">throws<\/span> <span class=\"nc\">InterruptedException<\/span><span class=\"o\">,<\/span> <span class=\"nc\">ExecutionException<\/span><span class=\"o\">,<\/span> <span class=\"nc\">TimeoutException<\/span><span class=\"o\">;<\/span>\r\n<span class=\"o\">}<\/span>\r\n\r\n<\/code><\/pre>\n<h1>\u4ee5\u4e0b\u662f\u4f7f\u7528\u793a\u4f8b\u3002<\/h1>\n<pre class=\"post-pre\"><code>\r\n<span class=\"kn\">import<\/span> <span class=\"nn\">java.util.concurrent.*<\/span><span class=\"o\">;<\/span>\r\n\r\n\r\n<span class=\"kd\">class<\/span> <span class=\"nc\">Caller<\/span> <span class=\"kd\">implements<\/span> <span class=\"nc\">Callable<\/span><span class=\"o\">&lt;<\/span><span class=\"nc\">Integer<\/span><span class=\"o\">&gt;<\/span> <span class=\"o\">{<\/span>\r\n\r\n    <span class=\"kd\">public<\/span> <span class=\"nc\">Integer<\/span> <span class=\"nf\">call<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span>\r\n        <span class=\"nc\">System<\/span><span class=\"o\">.<\/span><span class=\"na\">out<\/span><span class=\"o\">.<\/span><span class=\"na\">println<\/span><span class=\"o\">(<\/span><span class=\"s\">\"call\"<\/span><span class=\"o\">);<\/span>\r\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"o\">;<\/span>\r\n    <span class=\"o\">}<\/span>\r\n<span class=\"o\">}<\/span>\r\n\r\n<span class=\"kd\">class<\/span> <span class=\"nc\">Runner<\/span> <span class=\"kd\">implements<\/span> <span class=\"nc\">Runnable<\/span> <span class=\"o\">{<\/span>\r\n    <span class=\"kd\">public<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">run<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span>\r\n        <span class=\"nc\">System<\/span><span class=\"o\">.<\/span><span class=\"na\">out<\/span><span class=\"o\">.<\/span><span class=\"na\">println<\/span><span class=\"o\">(<\/span><span class=\"s\">\"run\"<\/span><span class=\"o\">);<\/span>\r\n    <span class=\"o\">}<\/span>\r\n<span class=\"o\">}<\/span>\r\n\r\n<span class=\"kd\">public<\/span> <span class=\"kd\">class<\/span> <span class=\"nc\">Sample<\/span> <span class=\"o\">{<\/span>\r\n\r\n    <span class=\"kd\">public<\/span> <span class=\"kd\">static<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">main<\/span><span class=\"o\">(<\/span><span class=\"nc\">String<\/span><span class=\"o\">[]<\/span> <span class=\"n\">args<\/span><span class=\"o\">)<\/span> <span class=\"kd\">throws<\/span> <span class=\"nc\">InterruptedException<\/span><span class=\"o\">,<\/span> <span class=\"nc\">ExecutionException<\/span> <span class=\"o\">{<\/span>\r\n        <span class=\"nc\">ExecutorService<\/span> <span class=\"n\">executorService<\/span> <span class=\"o\">=<\/span> <span class=\"nc\">Executors<\/span><span class=\"o\">.<\/span><span class=\"na\">newFixedThreadPool<\/span><span class=\"o\">(<\/span><span class=\"mi\">2<\/span><span class=\"o\">);<\/span>\r\n        <span class=\"nc\">Future<\/span> <span class=\"n\">f1<\/span> <span class=\"o\">=<\/span> <span class=\"n\">executorService<\/span><span class=\"o\">.<\/span><span class=\"na\">submit<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"nc\">Runner<\/span><span class=\"o\">());<\/span>\r\n        <span class=\"nc\">Future<\/span> <span class=\"n\">f2<\/span> <span class=\"o\">=<\/span> <span class=\"n\">executorService<\/span><span class=\"o\">.<\/span><span class=\"na\">submit<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"nc\">Caller<\/span><span class=\"o\">());<\/span>\r\n        <span class=\"nc\">System<\/span><span class=\"o\">.<\/span><span class=\"na\">out<\/span><span class=\"o\">.<\/span><span class=\"na\">println<\/span><span class=\"o\">(<\/span><span class=\"n\">f1<\/span><span class=\"o\">.<\/span><span class=\"na\">get<\/span><span class=\"o\">()<\/span> <span class=\"o\">+<\/span> <span class=\"s\">\":\"<\/span> <span class=\"o\">+<\/span> <span class=\"n\">f2<\/span><span class=\"o\">.<\/span><span class=\"na\">get<\/span><span class=\"o\">());<\/span>  <span class=\"c1\">\/\/ =&gt; null:0<\/span>\r\n    <span class=\"o\">}<\/span>\r\n<span class=\"o\">}<\/span>\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java API\u4ee3\u7801 public interface Future&lt;V&gt; { \/** * Att [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-31640","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u7684Future\u63a5\u53e3 - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/zh\/blog\/java\u7684future\u63a5\u53e3\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java\u7684Future\u63a5\u53e3\" \/>\n<meta property=\"og:description\" content=\"Java API\u4ee3\u7801 public interface Future&lt;V&gt; { \/** * Att [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/zh\/blog\/java\u7684future\u63a5\u53e3\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-17T12:22:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-14T22:50:17+00:00\" \/>\n<meta name=\"author\" content=\"\u9038, \u79d1\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u9038, \u79d1\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/\",\"name\":\"Java\u7684Future\u63a5\u53e3 - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\"},\"datePublished\":\"2023-03-17T12:22:09+00:00\",\"dateModified\":\"2024-01-14T22:50:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/85c1dae56e6ea1e695c73d33c684d487\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/zh\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u7684Future\u63a5\u53e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/85c1dae56e6ea1e695c73d33c684d487\",\"name\":\"\u9038, \u79d1\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c94f6d9cbbfbca863fab309840bd690c153c95f8490c290ad2ed54dd693dad16?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c94f6d9cbbfbca863fab309840bd690c153c95f8490c290ad2ed54dd693dad16?s=96&d=mm&r=g\",\"caption\":\"\u9038, \u79d1\"},\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/author\/keyi\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java\u7684Future\u63a5\u53e3 - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/zh\/blog\/java\u7684future\u63a5\u53e3\/","og_locale":"zh_CN","og_type":"article","og_title":"Java\u7684Future\u63a5\u53e3","og_description":"Java API\u4ee3\u7801 public interface Future&lt;V&gt; { \/** * Att [&hellip;]","og_url":"https:\/\/www.silicloud.com\/zh\/blog\/java\u7684future\u63a5\u53e3\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2023-03-17T12:22:09+00:00","article_modified_time":"2024-01-14T22:50:17+00:00","author":"\u9038, \u79d1","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u9038, \u79d1","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/","url":"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/","name":"Java\u7684Future\u63a5\u53e3 - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website"},"datePublished":"2023-03-17T12:22:09+00:00","dateModified":"2024-01-14T22:50:17+00:00","author":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/85c1dae56e6ea1e695c73d33c684d487"},"breadcrumb":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/zh\/blog\/"},{"@type":"ListItem","position":2,"name":"Java\u7684Future\u63a5\u53e3"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website","url":"https:\/\/www.silicloud.com\/zh\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/85c1dae56e6ea1e695c73d33c684d487","name":"\u9038, \u79d1","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c94f6d9cbbfbca863fab309840bd690c153c95f8490c290ad2ed54dd693dad16?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c94f6d9cbbfbca863fab309840bd690c153c95f8490c290ad2ed54dd693dad16?s=96&d=mm&r=g","caption":"\u9038, \u79d1"},"url":"https:\/\/www.silicloud.com\/zh\/blog\/author\/keyi\/"},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/java%e7%9a%84future%e6%8e%a5%e5%8f%a3\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/31640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/comments?post=31640"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/31640\/revisions"}],"predecessor-version":[{"id":57111,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/31640\/revisions\/57111"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/media?parent=31640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/categories?post=31640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/tags?post=31640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}