Callable 和Runnable
1:Callable ,方法调用会有返回值。
private void callableTest throws ExecutionException, InterruptedException {
ExecutorService executorService=null;
Future<Person> future = executorService.submit(new Callable<Person>() { //executorService 执行 Callable. Future是一个异步结果的容器。
@Override
public Person call() throws Exception {
return new Person();
}
});
Person person = future.get(); //异步结果,可以用于中断。和FutureTask不同。 FutureTask实现了Runnable,可以是一个任务,可以通过.get获取将来的结果。
}
2Runnable
private void runnableTest() throws ExecutionException, InterruptedException {
ExecutorService executorService=null;
Future<?> future = executorService.submit(new Runnable() {
@Override
public void run() {
return;
}
});
Object o = future.get();
}
Callable 和Runnable的更多相关文章
- Callable 和 Runnable 的区别
Callable 和 Runnable 的使用方法大同小异, 区别在于: 1.Callable 使用 call() 方法, Runnable 使用 run() 方法 2.call() 可以返回值, 而 ...
- Callable和Runnable和FutureTask
http://www.cnblogs.com/dolphin0520/p/3949310.html 一.Callable与Runnable 二.Future 三.FutureTask 四.使用示例 一 ...
- 在线程池使用Callable和Runnable的区别以及如何关闭线程
一.区别总结: Callable定义的方法是call,而Runnable定义的方法是run. Callable的call方法可以有返回值,而Runnable的run方法不能有返回值,这是核心区别. C ...
- 深入Callable及Runnable两个接口 获取线程返回结果
今天碰到一个需要获取线程返回结果的业务场景,所以了解到了Callable接口. 先来看下下面这个例子: public class ThreadTest { public static void mai ...
- callable和runnable的区别
Runnable接口源码 @FunctionalInterface public interface Runnable { /** * When an object implementing inte ...
- Callable, Runnable, Future, FutureTask
Java并发编程之Callable, Runnable, Future, FutureTask Java中存在Callable, Runnable, Future, FutureTask这几个与线程相 ...
- Runnable、Callable、Future和FutureTask用法
http://www.cnblogs.com/dolphin0520/p/3949310.html java 1.5以前创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable ...
- java并发编程--Runnable Callable及Future
1.Runnable Runnable是个接口,使用很简单: 1. 实现该接口并重写run方法 2. 利用该类的对象创建线程 3. 线程启动时就会自动调用该对象的run方法 通常在开发中结合Execu ...
- java多线程—Runnable、Thread、Callable区别
多线程编程优点 进程之间不能共享内存,但线程之间共享内存非常容易. 系统创建线程所分配的资源相对创建进程而言,代价非常小. Java中实现多线程有3种方法: 继承Thread类 实现Runnable接 ...
随机推荐
- schema in oracle
the conception of schema is different in different db software. here i just refer to oracle schema. ...
- swift - UIAlertController 的用法
ios 8 以后苹果官方建议使用UIAlertController这个类,所以专门去网上找资料,了解了下用法, 1.创建一个alertController let alertController = ...
- php-新特性,生成器的创建和使用
mark 一下~ http://laravelacademy.org/post/4317.html
- laravel 使用 session
配置方面的不写了,请参考学院君的文章:http://laravelacademy.org/post/5898.html 在开始之前先说一下,使用 request 对象的 session() 方法,和直 ...
- int()
int() 用于将一个对象转换为整数,可转换的对象如下: In [1]: int(') # 将纯数字的字符串转换为整数 Out[1]: 10 In [2]: int(10.6) # 将浮点数转换为整数 ...
- oracle 与mysql 的当前时间比较
select p.id,p.order_Num,p.image_url,p.url,p.image_topic, p.is_download, p.big_image_url, p.begin_tim ...
- 《C++ Primer Plus》第7章 函数——C++的编程模块 学习笔记
函数是C++的编程模块.要使用函数,必须提供定义和原型,并调用该函数.函数定义是实现函数功能的代码:函数原型描述了函数的接口:传递给函数的值的书目和种类以及函数的返回类型.函数调用使得程序将参数传递给 ...
- Weui upLoader
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android 动画fillAfter和fillBefore
fillBefore是指动画结束时画面停留在此动画的第一帧; fillAfter是指动画结束是画面停留在此动画的最后一帧. Java代码设置如下: /*****动画结束时,停留在最后一帧******* ...
- SpringData JPA查询分页demo
SpringData JPA 的 PagingAndSortingRepository接口已经提供了对分页的支持,查询的时候我们只需要传入一个 org.springframework.data.dom ...