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的调用都会发生阻塞直到结果可获得为止,当然,线程是并行运行的,因此,很可能在大致相同的时刻所有的结果都可获得。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 团宠崽崽是只桃花精 我那老板柔弱不能自理 反派小媳妇的逆袭指南 我有一个大佬群 二婚后,我在豪门扮柔弱 诸天仙神热搜:主神老婆竟是魔尊 团宠小福妻又娇又软 替嫁医妃有空间 重生之农门贵夫 野性偏爱 嫁给病弱太子后我躺赢了 咸鱼娘娘一心只想翻墙 救命!破产后高冷总裁处处招惹我 冷酷将军每天都想要贴贴 何以赎光 甜腻!病娇傅少竟然暗恋我! 天价萌妻:偏执帝少心尖宠 有读心术后,战神把娘子宠上天 穿书后,我刷错了反派的好感度 摄政王怀里的团宠美人娇又软 满级千金不想掉马 女帝她就是个卖药的! 华娱激荡年代 她来看我的演唱会 快穿:娇养反派大佬做替身 不努力种田就要和相公继承皇位了 穿书末世之我是金手指 转生恶少后的魔幻日常 穿书后我成了反派男二的铁血妈粉 带着超市重返年代 新婚夜,我被冷冰冰的王爷读心了 修仙女配拿了反派剧本 影后的假面童话 克死前夫后我成了心软的神 新婚夜,我治好了失明太子的隐疾 沦陷野玫瑰 完球!我的崽居然是反派 重生后她被憨憨相公娇宠了 师祖回归,徒弟各个是疯批大佬 植物人老公苏醒后,她只想逃 他怎么可能是魔尊