Runnbale封装一个异步运行的任务,可以把它想象成一个没有任何参数和返回值的异步方法。Callable和Runnable相似,但是它有返回值。Callable接口是参数化的类型,只有一个方法call

public interface Callable<V> {

V call() throws Exception;

}

类型参数就是返回值的类型,例如:Callable<String>表示最终返回一个String的异步操作(计算)

Runnbale封装一个异步运行的任务,可以把它想象成一个没有任何参数和返回值的异步方法。Callable和Runnable相似,但是它有返回值。Callable接口是参数化的类型,只有一个方法call()

public interface Callable<V> {

V call() throws Exception;

}

类型参数就是返回值的类型,例如:Callable<String>表示最终返回一个String的异步操作(计算)

    //求素数
class PrimeCallable implements Callable<int[]> {
private int max; public PrimeCallable(int max) {
this.max = max;
} @Override
public int[] call() throws Exception {
List<Integer> result = new ArrayList<Integer>();
for(int i = ; i <= max; i++) {
System.out.println("System is checking data " + i);
if(isPrime(i)) {
result.add(i);
}
} Integer[] iters = result.toArray(new Integer[]{});
int[] array = new int[iters.length];
int i = ;
for(Integer iter : iters) {
array[i++] = iter;
}
return array;
} private boolean isPrime(int data) {
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i = ; i < ((int)Math.sqrt(data)+); i++) {
if(data % i == )
return false;
}
return true;
}
}
    Callable<int[]> primeCallable = new PrimeCallable();
FutureTask<int[]> ftask = new FutureTask<int[]>(primeCallable); Thread t = new Thread(ftask);
t.start(); int[] result = null;
System.out.println("Waiting for result.....");
try {
result = ftask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} for(int i = ; result != null && i < result.length; i++) {
if(i != && i % == ) {
System.out.println();
}
System.out.print(String.format("%1$-5s", result[i]));
}
执行结果:
Waiting for result.....
System is checking data
System is checking data
System is checking data
System is checking data
System is checking data
System is checking data
..................................
System is checking data
System is checking data

最新文章

  1. 附加属性出现Failed to assign to property的问题
  2. 按下enter键后表单自动提交问题
  3. BZOJ3189 : [Coci2011]Slika
  4. SqlServer 在创建数据库时候指定的初始数据库大小是不能被收缩的
  5. PDO进行sql报表编制结果集介绍及操作(两)
  6. 201521123042 《java程序设计》 第八周学习总结
  7. python之decode、encode及codecs模块
  8. ubuntu16.04 下安装 visual studio code 以及利用 g++ 运行 c++程序
  9. 单例模式写MySQL model类,简单的增、删、改、查
  10. Java中try catch finally语句中含有return语句的执行情况(总结版)
  11. 临时关闭Mysql ONLY_FULL_GROUP_BY
  12. [POST] What Is the Linux fstab File, and How Does It Work?
  13. shell编程: 获得目录下(包括子目录)所有文件名,路径和文件大小
  14. Spring-AOP的五种通知和切面的优先级、通知变量声明
  15. Windows下Anaconda安装 python + tensorflow CPU版
  16. 「题目代码」P1044~P1048(Java)
  17. call和apply第一个参数为null/undefined,函数this指向全局对象
  18. CentOS 6.9下KVM虚拟机通过virt-clone克隆虚拟机(转)
  19. FTP 服务器性能 测试点
  20. UIWebView分页显示

热门文章

  1. iOS-tableViewCell创建时添加一些动画
  2. 【转】GOOGLE-PROTOBUF与FLATBUFFERS数据的序列化和反序列化
  3. Visual Studio 2012,创建工程Build Driver,基于纯Source Code.
  4. WCF服务全局异常处理机制
  5. [C/C++] 智能指针学习
  6. dnsmasq-2.48没有ipset特性,安装dnsmasq-2.71来支持ipset
  7. Hyperledger Fabric架构详解
  8. 【可持久化线段树?!】rope史上最全详解
  9. IDEA 用maven创建web项目编译时不能发布resources中的文件
  10. php 上传csv文件