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接 ...
随机推荐
- Unity 移动端的复制这么写
游戏上线很久了,有些玩家慢慢就流失了,为了让刚流失的玩家再度回归所以做了召回功能!如果一个200级的玩家10天没上线且成功召回的,就会给予召回玩家丰厚的奖励! Q:那如何召回这个流失的玩家呢? A:召 ...
- Emulator Error: Could not load OpenGLES emulation library: Could not load DLL!
Copy the file below from SDK\tools\lib to SDK\tools. libEGL_translator.dlllibGLES_CM_translator.dlll ...
- 【RF库Collections测试】Get Dictionary Values
Name:Get Dictionary ValuesSource:Collections <test library>Arguments:[ dictionary ]Returns val ...
- Template类的使用指南【python】
转自:http://www.jb51.net/article/55011.htm
- Android package属性、package name和Application ID三者的联系及区别
package属性:在AndroidManifest.xml文件中. package name:模块结构的包名. Application ID:模块defaultConfig块下的applicatio ...
- mqtt 服务器与客户端通讯
mqtt 服务器与客户端通讯. 服务器端 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- Memcache命令及参数用法
Memcache命令:在linux下: # /usr/local/bin/memcached -d -m 128 -u root -l 192.168.0.10 -p 12121 -c 256 -P ...
- SpringMVC配置session过期拦截器,返回登录页面
spring-mvc.xml配置 <mvc:interceptors> <!-- session失效拦截器 --> <mvc:interceptor> <!- ...
- 使用vim-pathogen 进行插件管理
使用vim的插件管理器 pathogen进行vim的插件管理. 1. pathogen 管理插件 pathogen让每个插件占有一个单独的目录,解决了文件分散的问题.安装完 pathogen之后,只需 ...
- python实现HTTP代理的思路和Demo
一.首先什么是代理: 代理其实就是中间转发的那个玩意,所以在代码逻辑上也是如此的. 二.Python写http代理的基本逻辑: (1)接受浏览器发出的请求,解析,拼凑成该有的样子,然后使用套接字发出去 ...