Callable与Future
来源:互联网 发布:硬笔字帖软件 编辑:程序博客网 时间:2024/06/08 19:47
1.代码示例
这个程序是用来找到一个目录下的文件含有关键字的文件数。
package future;import java.io.*;import java.util.*;import java.util.concurrent.*;/** * @version 1.01 2012-01-26 * @author Cay Horstmann */public class FutureTest{ public static void main(String[] args) { try (Scanner in = new Scanner(System.in)) { System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): "); String directory = in.nextLine(); System.out.print("Enter keyword (e.g. volatile): "); String keyword = in.nextLine(); MatchCounter counter = new MatchCounter(new File(directory), keyword); FutureTask<Integer> task = new FutureTask<>(counter); Thread t = new Thread(task); t.start(); try { System.out.println(task.get() + " matching files."); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { } } }}/** * This task counts the files in a directory and its subdirectories that contain a given keyword. */class MatchCounter implements Callable<Integer>{ private File directory; private String keyword; /** * Constructs a MatchCounter. * @param directory the directory in which to start the search * @param keyword the keyword to look for */ public MatchCounter(File directory, String keyword) { this.directory = directory; this.keyword = keyword; } public Integer call() { int count = 0; try { File[] files = directory.listFiles(); List<Future<Integer>> results = new ArrayList<>(); for (File file : files) if (file.isDirectory()) { MatchCounter counter = new MatchCounter(file, keyword); FutureTask<Integer> task = new FutureTask<>(counter); results.add(task); Thread t = new Thread(task); t.start(); } else { if (search(file)) count++; } for (Future<Integer> result : results) try { count += result.get(); } catch (ExecutionException e) { e.printStackTrace(); } } catch (InterruptedException e) { } return count; } /** * Searches a file for a given keyword. * @param file the file to search * @return true if the keyword is contained in the file */ public boolean search(File file) { try { try (Scanner in = new Scanner(file, "UTF-8")) { boolean found = false; while (!found && in.hasNextLine()) { String line = in.nextLine(); if (line.contains(keyword)) found = true; } return found; } } catch (IOException e) { return false; } }}每一次对get的调用都会发生阻塞直到结果可获得为止,当然,线程是并行运行的,因此,很可能在大致相同的时刻所有的结果都可获得。
阅读全文
0 0
- Callable 与 Future
- Callable与Future
- Callable与Future
- Callable与future
- Callable与Future
- Callable与future接口
- 浅谈Future与Callable
- Callable与Future
- Callable与Future
- Callable与Future
- Callable与Future
- Callable与Future
- Callable与Future
- Callable与Future
- Callable与Future的应用
- Java接口:Callable 与 Future
- 黑马程序员-Callable与Future
- Callable与Future的介绍
- Collection和Map
- 矩阵快速幂之整数快速幂
- 使用ansible yum依赖包,报错No module named yum
- 算法
- re
- Callable与Future
- Character 类
- 今日头条2017秋招前端工程师笔试试卷
- Java使用POI导出Word文档
- 连接Oracle数据库——接触oracle的第二天
- 白话ASP.NET MVC之三:Controller是如何解析出来的
- 第四章 Thread Executors(Executors多线程架构)【中】
- linux(cenOS6.5)卸载自带openJDK并安装JDK
- android view surfaceView GLSurfaceView