1. 程式人生 > >藉助執行緒池同步查詢檔案內容

藉助執行緒池同步查詢檔案內容

package  multiplethread;   import  java.io.File; import  java.io.FileReader; import  java.io.IOException;   public  class  SearchFileTask  implements  Runnable{        private  File file;
     private  String search;      public  SearchFileTask(File file,String search) {          this .file = file;          this .search= search;     
}            public  void  run(){                    String fileContent = readFileConent(file);         
if (fileContent.contains(search)){              System.out.printf(  "執行緒: %s 找到子目標字串%s,在檔案:%s%n" ,Thread.currentThread().getName(), search,file);                        }      }            public  String readFileConent(File file){          try  (FileReader fr =  new  FileReader(file)) {              char [] all =  new  char [( int ) file.length()];              fr.read(all);              return  new  String(all);          catch  (IOException e) {              e.printStackTrace();              return  null ;          }         }     }         package  multiplethread;    import  java.util.LinkedList;    public  class  ThreadPool {      // 執行緒池大小      int  threadPoolSize;         // 任務容器      LinkedList<Runnable> tasks =  new  LinkedList<Runnable>();         // 試圖消費任務的執行緒         public  ThreadPool() {          threadPoolSize =  10 ;             // 啟動10個任務消費者執行緒          synchronized  (tasks) {              for  ( int  i =  0 ; i < threadPoolSize; i++) {                  new  TaskConsumeThread( "任務消費者執行緒 "  + i).start();              }          }      }         public  void  add(Runnable r) {          synchronized  (tasks) {              tasks.add(r);              // 喚醒等待的任務消費者執行緒              tasks.notifyAll();          }      }         class  TaskConsumeThread  extends  Thread {          public  TaskConsumeThread(String name) {              super (name);          }             Runnable task;             public  void  run() {              while  ( true ) {                  synchronized  (tasks) {                      while  (tasks.isEmpty()) {                          try  {                              tasks.wait();                          catch  (InterruptedException e) {                              // TODO Auto-generated catch block                              e.printStackTrace();                          }                      }                      task = tasks.removeLast();                      // 允許新增任務的執行緒可以繼續新增任務                      tasks.notifyAll();                     }                  task.run();              }          }      }    }     package  multiplethread;     import  java.io.File;     public  class  TestThread {          static  ThreadPool pool=  new  ThreadPool();      public  static  void  search(File file, String search) {                    if  (file.isFile()) {              if (file.getName().toLowerCase().endsWith( ".java" )){                  SearchFileTask task =  new  SearchFileTask(file, search);                  pool.add(task);              }          }          if  (file.isDirectory()) {              File[] fs = file.listFiles();              for  (File f : fs) {                  search(f, search);              }          }      }              public  static  void  main(String[] args) {          File folder = new  File( "e:\\project" );          search(folder, "Magic" );      } }