ImageVerifierCode 换一换
格式:DOCX , 页数:25 ,大小:23.50KB ,
资源ID:17897484      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/17897484.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java并发编程实践Callable异步回调FutureFutureTask用法Word格式文档下载.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java并发编程实践Callable异步回调FutureFutureTask用法Word格式文档下载.docx

1、Future represents the result of an asynchronous * computation. Methods are provided to check if the computation is * complete, to wait for its completion, and to retrieve the result of * the computation. The result can only be retrieved using method * get when the computation has completed, blocking

2、 if * necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to * determine if the task completed normally or was cancelled. Once a * computation has completed, the computation cannot be cancelled. * If you would like to use a for the sake * of c

3、ancellability but not provide a usable result, you can * declare types of the form code Future and * return nullbSample Usage (Note that the following classes are all * made-up.) code * interface ArchiveSearcher String search(String target); * class App * ExecutorService executor = . * ArchiveSearch

4、er searcher = . * void showSearch(final String target) * throws InterruptedException * Future future * = executor.submit(new Callable() * public String call() * return searcher.search(target); * ); * displayOtherThings(); / do other things while searching * try * displayText(future.get(); / use futu

5、re * catch (ExecutionException ex) cleanup(); return; * * * The link FutureTask class is an implementation of that * implements Runnable, and so may be executed by an Executor. * For example, the above construction with submit could be replaced by: * FutureTask future = * new FutureTask(new Callable

6、 * executor.execute(future);Memory consistency effects: Actions taken by the asynchronous computation a href=package-summary.html#MemoryVisibility happen-before * actions following the corresponding code Future.get() in another thread. * see FutureTask * see Executor * since 1.5 * author Doug Lea *

7、param The result type returned by this Futures method public interface Future /* * Attempts to cancel execution of this task. This attempt will * fail if the task has already completed, has already been cancelled, * or could not be cancelled for some other reason. If successful, * and this task has

8、not started when is called, * this task should never run. If the task has already started, * then the mayInterruptIfRunning parameter determines * whether the thread executing this task should be interrupted in * an attempt to stop the task. After this method returns, subsequent calls to link #isDon

9、e will * always return true. Subsequent calls to link #isCancelled * will always return if this method returned * param mayInterruptIfRunning if the thread executing this * task should be interrupted; otherwise, in-progress tasks are allowed * to complete * return false if the task could not be canc

10、elled, * typically because it has already completed normally; otherwise boolean cancel(boolean mayInterruptIfRunning); * Returns if this task was cancelled before it completed * normally. boolean isCancelled(); if this task completed. * Completion may be due to normal termination, an exception, or *

11、 cancellation - in all of these cases, this method will return if this task completed boolean isDone(); * Waits if necessary for the computation to complete, and then * retrieves its result. * return the computed result * throws CancellationException if the computation was cancelled * throws Executi

12、onException if the computation threw an * exception * throws InterruptedException if the current thread was interrupted * while waiting V get() throws InterruptedException, ExecutionException; * Waits if necessary for at most the given time for the computation * to complete, and then retrieves its r

13、esult, if available. * param timeout the maximum time to wait * param unit the time unit of the timeout argument * throws TimeoutException if the wait timed out V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; Callable返回Future示例import java.util.co

14、ncurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit; * Callable的Future用法 * package .CallableDemo * date 2017年4月5日 下午2:53:18 * author pengjunlin * comment * update public class Call

15、ableFuture * param args * throws Exception * throws InterruptedException public static void main(String args) throws InterruptedException, Exception / TODO Auto-generated method stub ExecutorService exec = Executors.newCachedThreadPool(); / Future是一个接口,该接口用来返回异步的结果。 Future st = exec.submit(new TaskC

16、allable(); /* 同步结果,并且设置超时时间 */ System.out.println(st.get(10000, TimeUnit.MILLISECONDS); System.out.println(finished);class TaskCallable implements Callable public String call() throws Exception Thread.sleep(1000); return callstatus=OK;FutureTaskFutureTask实现了java.util.concurrent.RunnableFuture接口,实际上实

17、现了Runnable和 Future两个接口。FutureTask源码import java.util.concurrent.locks.LockSupport; * A cancellable asynchronous computation. This class provides a base * implementation of link Future, with methods to start and cancel * a computation, query to see if the computation is complete, and * retrieve the re

18、sult of the computation. The result can only be * retrieved when the computation has completed; the code get * methods will block if the computation has not yet completed. Once * the computation has completed, the computation cannot be restarted * or cancelled (unless the computation is invoked usin

19、g * link #runAndReset). A code FutureTask can be used to wrap a link Callable or * link Runnable object. Because code FutureTask implements * code Runnable, a code FutureTask can be submitted to an * link Executor for execution. In addition to serving as a standalone class, this class provides * cod

20、e protected functionality that may be useful when creating * customized task classes. The result type returned by this FutureTasks code get methods public class FutureTask implements RunnableFuture /* * Revision notes: This differs from previous versions of this * class that relied on AbstractQueued

21、Synchronizer, mainly to * avoid surprising users about retaining interrupt status during * cancellation races. Sync control in the current design relies * on a state field updated via CAS to track completion, along * with a simple Treiber stack to hold waiting threads. * Style note: As usual, we byp

22、ass overhead of using * AtomicXFieldUpdaters and instead directly use Unsafe intrinsics. * The run state of this task, initially NEW. The run state * transitions to a terminal state only in methods set, * setException, and cancel. During completion, state may take on * transient values of COMPLETING (while outcome is being set) or * INTERRUPTING (only while interrupting the runner to satisfy a * cancel(true). Transitions from these intermediate to final * states use cheaper ordered/lazy writes because values are uniq

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1