Callable、Future和FutureTask使用说明
boolean cancel(boolean mayInterruptIfRunning);- 参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务。
- 如果任务已经完成,则无论mayInterruptIfRunning为true还是false,此方法肯定返回false,即如果取消已经完成的任务会返回false;
- 如果任务正在执行,若mayInterruptIfRunning设置为true,则返回true,若mayInterruptIfRunning设置为false,则返回false;
- 如果任务还没有执行,则无论mayInterruptIfRunning为true还是false,肯定返回true。
boolean isCancelled();boolean isDone();V get() throws InterruptedException, ExecutionException;V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; public class CallableAndFuture1 { public static void main(String[] args) {
ExecutorService service = Executors.newSingleThreadExecutor(); Future<Integer> future = service.submit( new Callable<Integer>() { @Override
public Integer call() throws Exception {
System. out.println("子线程" + Thread.currentThread().getName() + "在进行计算");
Thread. sleep(3000); int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
return sum;
}
}); System. out.println("主线程" + Thread.currentThread ().getName() + "在执行任务" ); try {
System. out.println("子线程运行结果" + future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} System. out.println("所有任务执行完毕" ); }
}
Callable、Future和FutureTask使用说明的更多相关文章
- Java并发编程:ThreadPoolExecutor + Callable + Future(FutureTask) 探知线程的执行状况
如题 (总结要点) 使用ThreadPoolExecutor来创建线程,使用Callable + Future 来执行并探知线程执行情况: V get (long timeout, TimeUnit ...
- Java并发编程:Callable、Future和FutureTask
作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本博客中未标明转载的文章归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...
- Java多线程21:多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask
CyclicBarrier 接着讲多线程下的其他组件,第一个要讲的就是CyclicBarrier.CyclicBarrier从字面理解是指循环屏障,它可以协同多个线程,让多个线程在这个屏障前等待,直到 ...
- Callable、Future和FutureTask
创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就必须通过共享变量或者使用线 ...
- Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要
Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...
- Java并发:Callable、Future和FutureTask
Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...
- Runnable、Callable、Future和FutureTask用法
http://www.cnblogs.com/dolphin0520/p/3949310.html java 1.5以前创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable ...
- Callable、Future和FutureTask区别
在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...
- Java并发编程:Callable、Future和FutureTask的实现
启动线程执行任务,如果需要在任务执行完毕之后得到任务执行结果,可以使用从Java 1.5开始提供的Callable和Future 下面就分析一下Callable.Future以及FutureTask的 ...
随机推荐
- Install minidwep-gtk
Hi to everyone in this time i'm going to show you how to install minidwep-gtk to test your own wifi ...
- ios开发中,A valid provisioning profile for this executable was not found,的解决方法
手头上的一个ios项目在上架后,再进行时出现了以上的这个错误,这是上架后忘了对一些配置进行复原 我的项目解决方法是: 是上面的这一块出现了问题,图片上的配置是正常的情况,但是上架的时候对其进行了修改, ...
- 自己安装的几个Eclipse插件
http://eclipsenotepad.sourceforge.net This plugin has the simple objective to let developers write s ...
- const,readonly,static
1.const 表示的是常量(constant),始终不会发生改变,在编译时就确定了.所以类中定义一个常量可以被类访问也可以被类的实例访问.定义时就不能和static一起用.如果用了也是没有作用的,所 ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- poll()
# include < sys/ poll. h> int poll ( struct pollfd * fds, unsigned int nfds, int timeout) ; 和s ...
- [OC Foundation框架 - 18] Class
使用Class来创建实例 // 18.通过@"Ball"创建一个Ball实例(不可以使用[[Ball alloc] init]创建) NSString *className = @ ...
- HDU 1018 Big Number
LINK:HDU 1018 题意:求n!的位数~ 由于n!最后得到的数是十进制,故对于一个十进制数,求其位数可以对该数取其10的对数,最后再加1~ 易知:n!=n*(n-1)*(n-2)*...... ...
- ObjC语法练习 冒泡排序、选择排序、矩阵相乘
用OC实现的冒泡排序.选择排序.矩阵相乘,纯粹是用来练习语法. 冒泡排序,程序如下: void bubbleSort() { //初始化数组 NSMutableArray *array1 = [[NS ...
- NOTES : A Model of Gas Exchange for Hyperpolarized Xe(129) Magnetic Resonance of the Lung
NOTES : A Model of Gas Exchange for Hyperpolarized Xe(129) Magnetic Resonance of the Lung 背景知识: Ga ...