executorcompletionservice.html
来自「j2se5-api-zh,java文档的中文版本」· HTML 代码 · 共 460 行 · 第 1/2 页
HTML
460 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc (build 1.5.0_03) on Wed Nov 02 09:51:41 CST 2005 --><META http-equiv="Content-Type" content="text/html; charset=gb2312"><TITLE>ExecutorCompletionService (Java 2 Platform SE 5.0)</TITLE><META NAME="keywords" CONTENT="java.util.concurrent.ExecutorCompletionService class"><LINK REL ="stylesheet" TYPE="text/css" HREF="../../../stylesheet.css" TITLE="Style"><SCRIPT type="text/javascript">function windowTitle(){ parent.document.title="ExecutorCompletionService (Java 2 Platform SE 5.0)";}</SCRIPT><NOSCRIPT></NOSCRIPT></HEAD><BODY BGCOLOR="white" onload="windowTitle();"><!-- ========= START OF TOP NAVBAR ======= --><A NAME="navbar_top"><!-- --></A><A HREF="#skip-navbar_top" title="跳过导航链接"></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY=""><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY=""> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>概述</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>软件包</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>类</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/ExecutorCompletionService.html"><FONT CLASS="NavBarFont1"><B>使用</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>树</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>已过时</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>索引</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>帮助</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Standard Ed. 5.0</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="Executor.html" title="java.util.concurrent 中的接口"><B>上一个类</B></A> <A HREF="Executors.html" title="java.util.concurrent 中的类"><B>下一个类</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../../index.html@java_2Futil_2Fconcurrent_2FExecutorCompletionService.html" target="_top"><B>框架</B></A> <A HREF="ExecutorCompletionService.html" target="_top"><B>无框架</B></A> <SCRIPT type="text/javascript"> <!-- if(window==top) { document.writeln('<A HREF="../../../allclasses-noframe.html"><B>所有类</B></A>'); } //--></SCRIPT><NOSCRIPT> <A HREF="../../../allclasses-noframe.html"><B>所有类</B></A></NOSCRIPT></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> 摘要: 嵌套 | 字段 | <A HREF="#constructor_summary">构造方法</A> | <A HREF="#method_summary">方法</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">详细信息: 字段 | <A HREF="#constructor_detail">构造方法</A> | <A HREF="#method_detail">方法</A></FONT></TD></TR></TABLE><A NAME="skip-navbar_top"></A><!-- ========= END OF TOP NAVBAR ========= --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.util.concurrent</FONT><BR>类 ExecutorCompletionService<V></H2><PRE><A HREF="../../lang/Object.html" title="java.lang 中的类">java.lang.Object</A> <IMG SRC="../../../resources/inherit.gif" ALT="继承者 "><B>java.util.concurrent.ExecutorCompletionService<V></B></PRE><DL><DT><B>所有已实现的接口:</B> <DD><A HREF="CompletionService.html" title="java.util.concurrent 中的接口">CompletionService</A><V></DD></DL><HR><DL><DT><PRE>public class <B>ExecutorCompletionService<V></B><DT>extends <A HREF="../../lang/Object.html" title="java.lang 中的类">Object</A><DT>implements <A HREF="CompletionService.html" title="java.util.concurrent 中的接口">CompletionService</A><V></DL></PRE><P>使用提供的 <A HREF="Executor.html" title="java.util.concurrent 中的接口"><CODE>Executor</CODE></A> 来执行任务的 <A HREF="CompletionService.html" title="java.util.concurrent 中的接口"><CODE>CompletionService</CODE></A>。此类将安排那些完成时提交的任务,把它们放置在可使用 <tt>take</tt> 访问的队列上。该类非常轻便,适合于在执行几组任务时临时使用。 <p><b>用法示例。</b>假定您有针对某个问题的一组求解程序,每个求解程序都能返回某种类型的 <tt>Result</tt> 值,并且您想同时运行它们,使用方法 <tt>use(Result r)</tt> 处理返回非 null 值的每个求解程序的返回结果。可以这样编写程序: <pre> void solve(Executor e, Collection<Callable<Result>> solvers) throws InterruptedException, ExecutionException { CompletionService<Result> ecs = new ExecutorCompletionService<Result>(e); for (Callable<Result> s : solvers) ecs.submit(s); int n = solvers.size(); for (int i = 0; i < n; ++i) { Result r = ecs.take().get(); if (r != null) use(r); } } </pre>假定您想使用任务集中的第一个非 null 结果,而忽略任何遇到异常的任务,并且在第一个任务就绪时取消其他所有任务: <pre> void solve(Executor e, Collection<Callable<Result>> solvers) throws InterruptedException { CompletionService<Result> ecs = new ExecutorCompletionService<Result>(e); int n = solvers.size(); List<Future<Result>> futures = new ArrayList<Future<Result>>(n); Result result = null; try { for (Callable<Result> s : solvers) futures.add(ecs.submit(s)); for (int i = 0; i < n; ++i) { try { Result r = ecs.take().get(); if (r != null) { result = r; break; } } catch(ExecutionException ignore) {} } } finally { for (Future<Result> f : futures) f.cancel(true); } if (result != null) use(result); } </pre><P><P><HR><P><!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2"><B>构造方法摘要</B></FONT></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="ExecutorCompletionService.html#ExecutorCompletionService(java.util.concurrent.Executor)">ExecutorCompletionService</A></B>(<A HREF="Executor.html" title="java.util.concurrent 中的接口">Executor</A> executor)</CODE><BR> 使用为执行基本任务而提供的执行程序创建一个 ExecutorCompletionService,并将 <A HREF="LinkedBlockingQueue.html" title="java.util.concurrent 中的类"><CODE>LinkedBlockingQueue</CODE></A> 作为完成队列。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="ExecutorCompletionService.html#ExecutorCompletionService(java.util.concurrent.Executor, java.util.concurrent.BlockingQueue)">ExecutorCompletionService</A></B>(<A HREF="Executor.html" title="java.util.concurrent 中的接口">Executor</A> executor, <A HREF="BlockingQueue.html" title="java.util.concurrent 中的接口">BlockingQueue</A><<A HREF="Future.html" title="java.util.concurrent 中的接口">Future</A><<A HREF="ExecutorCompletionService.html" title="ExecutorCompletionService 中的类型参数">V</A>>> completionQueue)</CODE><BR> 使用为执行基本任务而提供的执行程序创建一个 ExecutorCompletionService,并将所提供的队列作为其完成队列。</TD></TR></TABLE> <!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2"><B>方法摘要</B></FONT></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> <A HREF="Future.html" title="java.util.concurrent 中的接口">Future</A><<A HREF="ExecutorCompletionService.html" title="ExecutorCompletionService 中的类型参数">V</A>></CODE></FONT></TD><TD><CODE><B><A HREF="ExecutorCompletionService.html#poll()">poll</A></B>()</CODE><BR> 检索并移除表示下一个已完成任务的 Future,如果不存在这样的任务,则返回 <tt>null</tt>。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> <A HREF="Future.html" title="java.util.concurrent 中的接口">Future</A><<A HREF="ExecutorCompletionService.html" title="ExecutorCompletionService 中的类型参数">V</A>></CODE></FONT></TD><TD><CODE><B><A HREF="ExecutorCompletionService.html#poll(long, java.util.concurrent.TimeUnit)">poll</A></B>(long timeout, <A HREF="TimeUnit.html" title="java.util.concurrent 中的枚举">TimeUnit</A> unit)</CODE><BR> 检索并移除表示下一个已完成任务的 Future,如果目前不存在这样的任务,则将等待指定的时间(如果有必要)。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> <A HREF="Future.html" title="java.util.concurrent 中的接口">Future</A><<A HREF="ExecutorCompletionService.html" title="ExecutorCompletionService 中的类型参数">V</A>></CODE></FONT></TD><TD><CODE><B><A HREF="ExecutorCompletionService.html#submit(java.util.concurrent.Callable)">submit</A></B>(<A HREF="Callable.html" title="java.util.concurrent 中的接口">Callable</A><<A HREF="ExecutorCompletionService.html" title="ExecutorCompletionService 中的类型参数">V</A>> task)</CODE><BR> 提交要执行的值返回任务,并返回表示挂起的任务结果的 Future。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> <A HREF="Future.html" title="java.util.concurrent 中的接口">Future</A><<A HREF="ExecutorCompletionService.html" title="ExecutorCompletionService 中的类型参数">V</A>></CODE></FONT></TD><TD><CODE><B><A HREF="ExecutorCompletionService.html#submit(java.lang.Runnable, V)">submit</A></B>(<A HREF="../../lang/Runnable.html" title="java.lang 中的接口">Runnable</A> task, <A HREF="ExecutorCompletionService.html" title="ExecutorCompletionService 中的类型参数">V</A> result)</CODE><BR> 提交要执行的 Runnable 任务,并返回一个表示任务完成的 Future,可以提取或轮询此任务。</TD></TR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?