java并发编程--Runnable Callable及Future
1.Runnable
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(new Runnable() {
public void run() {
//TODO
}
});
executor.shutdown();
public interface Callable<V> {
V call() throws Exception;
}
ExecutorService pool = Executors.newCachedThreadPool();
Future<String> future = pool.submit(new Callable{
public void call(){
//TODO
}
});
FutureTask<String> task = new FutureTask(new Callable{
public void call(){
//TODO
}
});
Thead thread = new Thread(task);
thread.start();
public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<T>(task, result);//通过RunnableAdapter实现
}
static final class RunnableAdapter<T> implements Callable<T> {
final Runnable task;
final T result;
RunnableAdapter(Runnable task, T result) {
this.task = task;
this.result = result;
}
public T call() {
task.run();
return result; //将传入的结果的直接返回
}
}
try{
future.get(60,TimeUtil.SECOND);
}catch(TimeoutException timeout){
log4j.log("任务越野,将被取消!!");
future.cancel();
}
FutureTask(Callable<V> callable)
FutureTask(Runnable runnable, V result)
5.应用
public class FileSearchTask {
public static void main(String[] args) throws ExecutionException, InterruptedException {
String path = args[0];
String keyword = args[1];
int c = 0;
File[] files = new File(path).listFiles();
ArrayList<Future<Integer>> rs = new ArrayList<>();
for(File file: files){ //每个文件启动一个task去查找
MatchCount count = new MatchCount();
count.file = file;
count.keyword = keyword;
FutureTask<Integer> task = new FutureTask(count);
rs.add(task); //将任务返回的结果添加到集合中
Thread thread = new Thread(task);
thread.start();
}
for(Future<Integer> f: rs){
c += f.get(); //迭代返回结果并累加
}
System.out.println("包含关键字的总文件数为:" + c);
}
}
class MatchCount implements Callable<Integer>{
public File file;
public String keyword;
private Integer count = 0;
public Integer call() throws Exception { //call封装线程所需做的任务
if(search(file))
count ++;
return count;
}
public boolean search(File file){
boolean founded = false;
try(Scanner scanner = new Scanner(new FileInputStream(file))){
while(!founded && scanner.hasNextLine()){
if (scanner.nextLine().contains(keyword))
founded = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return founded;
}
}
java并发编程--Runnable Callable及Future的更多相关文章
- Java并发编程:Callable、Future和FutureTask
作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本博客中未标明转载的文章归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...
- (转)Java并发编程:Callable、Future和FutureTask
Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...
- Java并发编程:Callable、Future和FutureTask(转)
Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...
- 15、Java并发编程:Callable、Future和FutureTask
Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...
- 007 Java并发编程:Callable、Future和FutureTask
原文https://www.cnblogs.com/dolphin0520/p/3949310.html Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述 ...
- Java 并发编程:Callable和Future
项目中经常有些任务需要异步(提交到线程池中)去执行,而主线程往往需要知道异步执行产生的结果,这时我们要怎么做呢?用runnable是无法实现的,我们需要用callable实现. import java ...
- Java并发编程:Callable、Future和FutureTask的实现
启动线程执行任务,如果需要在任务执行完毕之后得到任务执行结果,可以使用从Java 1.5开始提供的Callable和Future 下面就分析一下Callable.Future以及FutureTask的 ...
- [转载] Java并发编程:Callable、Future和FutureTask
转载自http://www.cnblogs.com/dolphin0520/p/3949310.html 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Run ...
- 【Java并发编程】Callable、Future和FutureTask的实现
启动线程执行任务,如果需要在任务执行完毕之后得到任务执行结果,可以使用从Java 1.5开始提供的Callable和Future 下面就分析一下Callable.Future以及FutureTask的 ...
随机推荐
- 小学生之浅谈Struts2与struts1的运行机制
Struts1工作原理图: 1.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(s ...
- mongodb的副本集总结
主节点A.备份节点B.仲裁者C.是否真的需求仲裁者? 需要. 怎样才算大多数表格如下: 怎样才算大多数表格 副本集中的成员总数 副本集中的大多数 1 1 2 2 3 2 4 3 5 3 6 4 7 4 ...
- (转)SQL流程控制语句学习(二):begin…end if…else case
1.begin…end 语法: begin {sql语句或语句块} end 注意:begin 和end要成对使用 2.if…else 语法: if 布尔表达式 {sql语句或语句块} else 布 ...
- java制作简单的坦克大战
坦克大战是我们小时候玩红白机时代的经典游戏,看到有不少小伙伴都使用各种语言实现了一下,手痒痒,也使用java做的一个比较简单的坦克大战,主要面向于学过Java的人群,与学了一段时间的人,有利于面向对象 ...
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...
- Linux 从 sar 到 sar2html 的认识
这些变形的工具.诸如:淘宝的Tsar.ksar.sar2html....等.都是通过抓取 sar的数据 所以在最终呈现的数据上变化不大.只是展现的手段和效果不一样而已 sar 是帮助我们 ...
- 转载:JSONObject.fromObject(map)(JSON与JAVA数据的转换)
转载网址:http://blog.sina.com.cn/s/blog_821025b70100wh6v.html JSON与JAVA数据的转换(JSON 即 JavaScript Object Na ...
- php stripslashes() addslashes() 解析
stripslashes() 函数删除由 addslashes() 函数添加的反斜杠. 实例: <?php $str = "Is your name O\'reilly?"; ...
- Python文件处理之文件读取方式(二)
Python的open文件的读取方式有以下几种方法: read([size]):读取文件,如果传了size参数,则读取size字节,否则读取全部 readline([size]):读取一行 readl ...
- string标准库的使用
string s; s.empty() 如果 s 为空串,则返回 true,否则返回 false. s.size() 返回 s 中字符的个数 s[n] 返回 s 中位置为 n 的字符,位置从 开始计数 ...