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线程池技术的 ...
随机推荐
- jquery接触初级-----juqery选择器实例
jquery 选择器用于触发事件,可以少写很多js代码,一般来说,基本的特效功能都能够完成 这里列举一个简单的jquery写的例子: 要求:有两种情况: 1.产品最初状态显示简约形式的品牌,即显示部分 ...
- THE BOX MODEL
Review In this lesson, we covered the four properties of the box model: height and width, padding, b ...
- setitimer函数
和alarm函数类似,都用于定时操作: 函数原型:int setitimer(int which, const struct itimerval *new_value, struct itimerva ...
- Python中os模块使用方法
os模块提供了对系统环境.文件.目录等操作系统级的接口函数.本文主要描述os模块和os.path模块常用函数以及常用实例. os模块函数 os.getcwd() 获取当前工作的目录. os.listd ...
- CHAR 和VARCHAR的区别
CHAR(10)是不可变长度为10的字符串,占的存储空间始终为10个字符的长度,而VARCHAR(10)是可变长度的字符串,故而可以节省空间.例如:储存"aaaaabbbbb",则 ...
- 浅析SQL Server 中的SOS_SCHEDULER_YIELD类型的等待
本文出处:http://www.cnblogs.com/wy123/p/6856802.html 进程的状态转换 在说明SOS_SCHEDULER_YIELD等待之前,先简要介绍一下进程的状态(迷迷糊 ...
- wParam与lParam的区别
wParam与lParam的区别 lParam 和 wParam 是宏定义,一般在消息函数中带这两个类型的参数,通常用来存储窗口消息的参数. LRESULT CALLBACK WindowProc(H ...
- Django中使用mysql数据库并使用原生sql语句操作
Django自身默认使用sqlite3这个轻量级的数据库,但是当我们开发网站时,sqlite3就没有mysql好,sqlite3适合一些手机上开发使用的数据库. 准备的软件mysql数据库,版本5.7 ...
- 跨年操作--new Date()
//时间在2017/12/31 17:00 --- 2018/1/1 06:00区间,提示用户无法操作公告. //time.js (function(){ var date = new Date(); ...
- xm数据写入
reshape有两个参数: 其中,参数:cn为新的通道数,如果cn = 0,表示通道数不会改变. 参数rows为新的行数,如果rows = 0,表示行数不会改变. 注意:新的行*列必须与原来的行*列相 ...