java 线程Thread 技术--1.5 Future与Callable
Callable:
从官方文档说起: 通过实现callable 的called 方法可以使一个任务可以返回一个结果以及可能抛出一个异常;
callable 与runnable 是相似的,可以被其他线程潜在的执行,但是runnable不会返回结果总是viod 以及不会抛出检测异常;
/**
* A task that returns a result and may throw an exception.
* Implementors define a single method with no arguments called
* {@code call}.
*
* <p>The {@code Callable} interface is similar to {@link
* java.lang.Runnable}, in that both are designed for classes whose
* instances are potentially executed by another thread. A
* {@code Runnable}, however, does not return a result and cannot
* throw a checked exception.
*
* <p>The {@link Executors} class contains utility methods to
* convert from other common forms to {@code Callable} classes.
一般我们是这么定义的:
class Task implements Callable<Integer>{
@Override
public Integer call() throws Exception {
return ;
}
}
但是我们如何能够获取到它的返回值?1.5 为我们提供了Future 接口,用于返回一个异步计算的结果,我们这样进行运行:
---线程池里去submit一个callable对象去执行;
public static void main(String[] args) throws Exception, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);
//执行callale task,返回future 对象
Future<Integer> submit = pool.submit(new Task());
System.out.println(submit);
Integer integer = submit.get();//obtain thread result ,is block
System.out.println(integer);
System.out.println(submit.isDone()) ;//is finish(trur or false)
}
我们追踪执行callable 的源码:发现内部使用了future实现类. FutureTask
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task); //-----------》 这里去new 了一个FutureTask(callable) ,并返回
execute(ftask); |
return ftask; |
} |
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
return new FutureTask<T>(callable); 《 ---------
}
可以看到,其实在 executor 内部运行的是FutureTask ,FutureTask 实现了Future 以及runnable 接口,所以可以获得异步执行的结果;
因为实现了runnable 接口,所以可以这么执行
public static void main(String[] args) throws InterruptedException, ExecutionException {
FutureTask<Integer> task =new FutureTask<>(new Task());
new Thread(task).start();
System.out.println(task.get());
}
也可以直接使用executor 的execute方法:
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);
FutureTask<Integer> task =new FutureTask<>(new Task());
pool.execute(task);
System.out.println(task.get());
}
接下来是一个小练习,比较单线程与多线程之间取1-20000之间的质数数量所花费的时间
package com.java.baseknowledge.concurrent15; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; /**
* 计算某一个区域里有多少个质数
* @author iscys
*
*/
public class ThreadPool3 { public static void main(String[] args) throws Exception, ExecutionException {
long start1 =System.currentTimeMillis();
List<Integer> zhishu = getZhishu(0,20000);
System.err.println(zhishu.size());
System.err.println("单线程"+(System.currentTimeMillis()-start1));
int thc = Runtime.getRuntime().availableProcessors();
ExecutorService pool = Executors.newFixedThreadPool(thc*2);
//lambda 表达式(param)->express
Future<List<Integer>> ta1 = pool.submit(()->getZhishu(0,8000));
//lambda 表达式(param)->{statement}
Future<List<Integer>> ta2 = pool.submit(()->{
System.out.println(Thread.currentThread().getName());
List<Integer> ss = getZhishu(8001,13000);
return ss;
});
Future<List<Integer>> ta3 = pool.submit(()->getZhishu(13001,17000));
Future<List<Integer>> ta4 = pool.submit(()->{ System.out.println(Thread.currentThread().getName()); List<Integer> ss = getZhishu(17001,20000); return ss; }); long start =System.currentTimeMillis();
List<Integer> list = ta1.get();
List<Integer> list2 = ta2.get();
List<Integer> list3 = ta3.get();
List<Integer> list4 = ta4.get();
System.out.println(list.size()+list2.size()+list3.size()+list4.size());
System.out.println(System.currentTimeMillis()-start);
pool.shutdown();
} static class MyTask implements Callable< List<Integer>>{
int start ,end;
MyTask(int start,int end){
this.start=start;
this.end=end;
}
@Override
public List<Integer> call() throws Exception {
// TODO Auto-generated method stub
return getZhishu(start,end);
} } static boolean isZhiShu(int num) { for(int i=2;i<=num/2;i++) {
if(num%i==0) {return false;}
}
return true;
} static List<Integer> getZhishu(int start,int end){ List<Integer> li =new ArrayList<Integer>(); for(int i=start;i<=end;i++) {
if(isZhiShu(i)) {li.add(i);}
}
return li;
}
}
java 线程Thread 技术--1.5 Future与Callable的更多相关文章
- java 线程Thread 技术--1.5 Executor Executors,ThreadPool,Queue
Executors : Executors ,就是一个线程工具类:大部分操作线程的方法,都可以在这个工具类中就行创建,执行,调用一些线程的方法: Executor : 用于执行和提交一个runnabl ...
- java 线程Thread 技术--线程状态与同步问题
线程技术第三篇: 线程的状态: 1. 创建状态: 当用new 操作符创建一个新的线程对象时,该线程就处于创建状态,系统不为它分配资源 2.可运行状态:当线程调用start 方法将为线程分配必须的系统资 ...
- java 线程Thread 技术--volatile关键字
java 语言中允许线程访问共享变量,为了保证共享变量能被准确和一致的更新,Java 语言提供了volatile 关键字,也就是我们所说的内存一致性: 问题抛出:(尝试去运行下面代码,以及将volat ...
- java 线程Thread 技术--1.5Lock 与condition 演示生产者与消费模式
在jdk 1.5 后,Java 引入了lock 锁来替代synchronized ,在使用中,lock锁的使用更加灵活,提供了灵活的 api ,不像传统的synchronized ,一旦进入synch ...
- java 线程Thread 技术--方法演示生产与消费模式
利用wait 与notifyAll 方法进行演示生产与消费的模式的演示,我们两个线程负责生产,两个线程消费,只有生产了才能消费: 在effective Java 中有说过: 1. 在Java 中 ,使 ...
- java 线程Thread 技术--线程创建源码解释
永远不要忘记最基础的东西,只有把最基础的知识打牢靠,才能够使你走的更远,我将从今天开始,进行线程知识的回顾,一些常用知识点,以及java1.5 引入的并发库,进行详细的讲解与总结 创建线程的目的是为了 ...
- java 线程Thread 技术--线程方法详解
Thread 类常用的方法与Object类提供的线程操作方法:(一个对象只有一把锁
- java 线程Thread 技术--创建线程的方式
在第一节中,对线程的创建我们通过看文档,得知线程的创建有两种方式进行实现,我们进行第一种方式的创建,通过继承Thread 类 ,并且重写它的run 方法,就可以进行线程的创建,所有的程序执行都放在了r ...
- java线程池技术(二): 核心ThreadPoolExecutor介绍
版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程池技术属于比较"古老"而又比较基础的技术了,本篇博客主要作用是个人技术梳理,没什么新玩意. 一.Java线程池技术的 ...
随机推荐
- NSNotification相关
NSNotification处理过程是一个同步的过程.它的消息回调函数执行的线程跟发送消息代码(也就是postNotification)所在的线程相同,一个Notification发出后,在回调函数执 ...
- 使用dig或nslookup指定dns服务器查询域名解析
一般来说linux下查询域名解析有两种选择,nslookup或者dig,而在使用上我觉得dig更加方便顺手.如果是在linux下的话,只要装上dnsutils这个包就可以使用dig命令, 安装bind ...
- SSL协议(安全套接层协议)
SSL是Secure Sockets Layer(安全套接层协议)的缩写,可以在Internet上提供秘密性传输.Netscape公司在推出第一个Web浏览器的同时,提出了SSL协议标准.其目标是保证 ...
- TFS登录时保存了用户密码,如何用其他账户登录
来源:http://blog.csdn.net/littlegreenfrog/article/details/5254633 使用TFS2008过程中,常常由于已经保存用户名和密码,却没有重新登 ...
- webkit内核自定义隐藏滚动条
1,在主页面可以拿到iframe,也可以为iframe注册onload等事件.document.getElementById('iframeId').onload 2,在主页面操作其中的iframe的 ...
- git工作操作步骤
上班开始,打开电脑,git pull:拉取git上最新的代码: 编辑代码,准备提交时,git stash:将自己编辑的代码暂存起来,防止git pull时与库中的代码起冲突,否则自己的代码就白敲了: ...
- 修饰词public、private、protected、默认、四者之间的区别
在Java语法中,对于类与类中的成员变量和成员方法是通过访问控制符来区分控制的. 下面来看看这四种访问控制符的区别:(public.protected.private.default) 1.publi ...
- 修改maven项目的编译版本
在pom.xml中添加如下代码 <build> <!-- 配置了很多插件 --> <plugins> <plugin> <groupId>o ...
- Oracle SCN机制解析
SCN(System Chang Number)作为oracle中的一个重要机制,在数据恢复.Data Guard.Streams复制.RAC节点间的同步等各个功能中起着重要作用.理解SCN的运作机制 ...
- xm数据写入
reshape有两个参数: 其中,参数:cn为新的通道数,如果cn = 0,表示通道数不会改变. 参数rows为新的行数,如果rows = 0,表示行数不会改变. 注意:新的行*列必须与原来的行*列相 ...