* Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。

* Callable和Runnable有几点不同:

* (1)Callable规定的方法是call(),而Runnable规定的方法是run().

* (2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。

* (3)call()方法可抛出异常,而run()方法是不能抛出异常的。

* (4)运行Callable任务可拿到一个Future对象,

* Future 表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。

* 通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果。

例子:

public class TimeOut {
public static void main(String[] args){
int timeout = ; //秒.
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result = false;
Future<Boolean> future = executor.submit(new MyJob("请求参数=="));// 将任务提交到线程池中
try {
result = future.get(timeout*, TimeUnit.MILLISECONDS);// 设定在200毫秒的时间内完成
System.out.println(result);
} catch (InterruptedException e) {
System.out.println("线程中断出错。");
future.cancel(true);// 中断执行此任务的线程
} catch (ExecutionException e) {
System.out.println("线程服务出错。");
future.cancel(true);// 中断执行此任务的线程
} catch (TimeoutException e) {// 超时异常
System.out.println("超时。");
future.cancel(true);// 中断执行此任务的线程
}finally{
System.out.println("线程服务关闭。");
executor.shutdown();
}
} static class MyJob implements Callable<Boolean> {
private String t;
public MyJob(String temp){
this.t= temp;
}
public Boolean call() {
//调整i大小测试超时
for(int i=;i<;i++){
if(i==){
System.out.println(t);
}
if (Thread.interrupted()){ //很重要
return false;
}
}
System.out.println("继续执行..........");
return true;
}
}
}
 public class TimeoutTest1 {

    public static void main(String[] args) {
final ExecutorService service = Executors.newFixedThreadPool();
TaskThread taskThread = new TaskThread();
System.out.println("提交任务...begin");
Future<Object> taskFuture = service.submit(taskThread);
System.out.println("提交任务...end");
try {
Object re = taskFuture.get(, TimeUnit.MILLISECONDS);// 超时设置,6s
System.out.println(re);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
System.out.println("超时 取消任务");
taskFuture.cancel(true);
System.out.println("超时 取消任务OK");
} finally {
service.shutdown();
}
}
} class TaskThread implements Callable<Object> {
public Object call() throws Exception {
String result = "空结果";
try {
System.out.println("任务开始....");
//修改sleep 的值测试超时
Thread.sleep();
result = "正确结果";
System.out.println("任务结束....");
} catch (Exception e) {
System.out.println("Task is interrupted!");
}
return result;
}
}

Callable 和 Future接口 学习的更多相关文章

  1. 使用Callable和Future接口创建线程

    具体是创建Callable接口的实现类,并实现clall()方法.并使用FutureTask类来包装Callable实现类的对象,且以此FutureTask对象作为Thread对象的target来创建 ...

  2. 基于java callable及future接口解决生产者消费者问题

    这两天复习java线程时,把java里面的线程基本知识点与jdk1.5以后新添加的一些类的使用都了解了一下,借用生产者消费者的问题来将他们实践一下. 题目:(题目在csdn一大牛的空间找的) 生产者- ...

  3. 【Java线程】Callable和Future

    Future模式 Future接口是Java线程Future模式的实现,可以来进行异步计算. Future模式可以这样来描述: 我有一个任务,提交给了Future,Future替我完成这个任务.期间我 ...

  4. Callable与Future的简单介绍

    Callable与Future的介绍 Callable与 Future 两功能是Java在后续版本中为了适应多并法才加入的,Callable是类似于Runnable的接口,实现Callable接口的类 ...

  5. Callable和Future详解

    Java程序员必须掌握的线程知识-Callable和Future Callable和Future出现的原因 创建线程的两种方式:继承Thread类和实现Runnable接口 这两种方式都有一种缺陷,执 ...

  6. Java线程和多线程(十三)——Callable,Future,FutureTask

    在Java多线程之中,Callable和Future的使用时非常广泛的.在之前的文章中,我们了解了关于Java线程池基础的一些内容,知道如何提交Runnable的任务.但是,Runnable的任务是无 ...

  7. 多线程-Thread,Runnable,Callable,Future,RunnableFuture,FutureTask

    类图: 先看各自的源码: public interface Runnable { public abstract void run(); } public class Thread implement ...

  8. Android(java)学习笔记66:实现Runnable接口创建线程 和 使用Callable和Future创建线程

    1. 前面说的线程的实现是新写一个子类继承Thread: 是将类声明为 Thread 的子类.该子类应重写 Thread 类的 run 方法.接下来可以分配并启动该子类的实例 2. 这里说的方案2是指 ...

  9. Android(java)学习笔记6:实现Runnable接口创建线程 和 使用Callable和Future创建线程

    1. 前面说的线程的实现是新写一个子类继承Thread: 是将类声明为 Thread 的子类.该子类应重写 Thread 类的 run 方法.接下来可以分配并启动该子类的实例 2. 这里说的方案2是指 ...

随机推荐

  1. 初始Jquery--以及工厂函数

    一.JavaScript框架 1什么是JavaScript框架 普通JavaScript的缺点:每种控件的操作方式不统一,不同浏览器下有区别,要编写跨浏览器的程序非常麻烦.因此出现了很多对JavaSc ...

  2. brew gradle

    cat /usr/local/Library/Taps/homebrew/homebrew-versions/gradle221.rb GRADLE_HOME=/Users/temp/gradle22 ...

  3. Code First04---关于上下文DbContext

    这章主要讲怎么配置DbContext的子类访问的数据库的位置. 我相信大家最经常使用的数据库位置的配置方式就是配置文件了,也就是通过App.Config 或Web.Config来配置要访问的数据库. ...

  4. AChartEngine 安卓折线图 柱形图等利器

    http://www.eoeandroid.com/thread-548233-1-6.html 最近公司项目中要用到折线图,状态类型的图标要用到折线图,柱形图等,并且能够动态显示,在网上找了许多de ...

  5. 转一篇简易易懂的android回调的实现--->(转的)

    回调机制在 Android 监听用户界面操作中的体现   本文讨论以下两个内容: 1. 回调函数 2. 回调机制在 Android框架 监听用户界面操作中的作用 一 回调函数 回调函数就是一个通过函数 ...

  6. Win7 x64bit安装Oracle10g

    解决方案:   步骤一:在解压出的oracle文件夹中搜索refhost.xml文件,搜索结果出现2条符合条件文件,这两个文件均需要修改.   打开文件发现内容中有包含...5.0 6.0等系统说明, ...

  7. [lintcode 14] First Position of Target

    For a given sorted array (ascending order) and a target number, find the first index of this number ...

  8. SSH-Struts第一弹:ActionSupport类

    Action继承了com.opensymphony.xwork2.ActionSupport. package com.candy.login; import com.opensymphony.xwo ...

  9. Eclipse J2EE LUNA 部署tomcat

  10. XsltListViewWebPart 和自定义列表视图

    http://msdn.microsoft.com/zh-cn/library/ff806162(v=office.14).aspx