Runnable任务没有返回值,而Callable任务有返回值。并且Callable的call()方法只能通过ExecutorService的submit(Callable <T> task) 方法来执行

public class RunnableTestMain {

    public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2); /**
* execute(Runnable x) 没有返回值。可以执行任务,但无法判断任务是否成功完成。
*/
pool.execute(new RunnableTest("Task1")); /**
* submit(Runnable x) 返回一个future。可以用这个future来判断任务是否成功完成。请看下面:
*/
Future future = pool.submit(new RunnableTest("Task2")); try {
if(future.get()==null){//如果Future's get返回null,任务完成
System.out.println("任务完成");
}
} catch (InterruptedException e) {
} catch (ExecutionException e) {
//否则我们可以看看任务失败的原因是什么
System.out.println(e.getCause().getMessage());
} } } public class RunnableTest implements Runnable { private String taskName; public RunnableTest(final String taskName) {
this.taskName = taskName;
} @Override
public void run() {
System.out.println("Inside "+taskName);
throw new RuntimeException("RuntimeException from inside " + taskName);
} }

转自:http://blog.163.com/huxb23@126/blog/static/625898182011121232077/

ExecutorService的submit(Runnable x)和execute(Runnable x) 两个方法的本质区别的更多相关文章

  1. ExecutorService.execute(Runnable x) 判断是否完成,得到返回值

    public class RunnableTestMain { public static void main(String[] args) { ExecutorService pool = Exec ...

  2. ExecutorService中submit()和execute()的区别

    在使用java.util.concurrent下关于线程池一些类的时候,相信很多人和我一样,总是分不清submit()和execute()的区别,今天从源码方面分析总结一下. 通常,我们通过Execu ...

  3. ExecutorService中submit和execute的区别(转)

    在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过Executor来启动线程比用Thread的start()更好.在新特征中 ...

  4. ExecutorService中submit和execute的区别

    在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过Executor来启动线程比用Thread的start()更好.在新特征中 ...

  5. 多线程ExecutorService中submit和execute区别

    submit和execute都是 ExecutorService 的方法,都是添加线程到线程池中. 区别 三个区别: 1.接收的参数不一样 2.submit有返回值,而execute没有 Method ...

  6. ExecutorService中submit和execute的区别<转>

    在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过Executor来启动线程比用Thread的start()更好.在新特征中 ...

  7. ExecutorService的submit方法使用

    在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过Executor来启动线程比用Thread的start()更好.在新特征中 ...

  8. Java线程池中submit()和execute()方法有什么区别

    两个方法都可以向线程池提交任务,execute()方法的返回类型是void,它定义在Executor接口中,而submit()方法返回有计算结构的Future对象,它定义在ExecutorServic ...

  9. ExecutorService的submit方法的坑

    先看一段代码: public Future<?> submit(Runnable task) { if (task == null) throw new NullPointerExcept ...

随机推荐

  1. 各种ps特效的网址

    粉笔字效果:http://www.jb51.net/photoshop/280740.html

  2. juit测试中报错:org.hibernate.HibernateException: Unable to get the default Bean Validation factory

    org.hibernate.HibernateException: Unable to get the default Bean Validation factory 解决方法: 解决方案: 在hib ...

  3. 清除HTML标记

    public static string DropHTML(string Htmlstring)        {            if (string.IsNullOrEmpty(Htmlst ...

  4. redis - 主从复制与主从切换

    redis2.8之前本身是不支持分布式管理的,一般建议使用redis3.0及以后版本 redis主从切换的方法 keepalive  或者 使用sentinel线程管理 说明如何使用sentinel实 ...

  5. zstu 4214 高楼扔鸡蛋(google 面试题)dp

    input T 1<=T<=10000 n m 1<=n<=2000000007 1<=m<=32 output m个鸡蛋从1到n哪一楼x扔下去刚好没碎,而再x+1 ...

  6. hadoop 2.2.0 关于map和reduce的个数的设置

    关于hadoop中的map过程,我的理解是每一个map系统会开启一个JVM进程来处理,map之间相互并行,map函数内串行.这样的想法是否正确? 由于想在hadoop集群上算一个初始输入数据不多,但是 ...

  7. HDU 2601 An easy problem

    (i+1)*(j+1)=n+1 转换成上面这个式子,也就是问n+1的因子有几个 #include<cstdio> #include<cstring> #include<c ...

  8. ZOJ 1655 FZU 1125 Transport Goods

    迪杰斯特拉最短路径. 1.every city must wait till all the goods arrive, and then transport the arriving goods t ...

  9. Node.js:包

    概要:本篇博客主要介绍了node.js中的包 包是在模块基础上更深一步的抽象,Node.js的包类似于C/C++的库函数或者Java/.Net的类库.它将某个独立的功能封装起来,用于发布.更新.依赖管 ...

  10. JS复习:第七章

    第七章  函数表达式 一.定义函数的方式有两种:函数声明和函数表达式. 1.函数声明: function functionName(arg0 , arg1 , arg2){ //函数体... } 函数 ...