zoukankan      html  css  js  c++  java
  • 借助线程池同步查找文件内容

    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");
        }
    }
  • 相关阅读:
    6.25作业
    博客园第一篇
    532. 数组中的K-diff数对
    echarts
    跨域问题
    数组中第三大的数 leetcode 414
    除自身以外数组的乘积leetcode 238
    xshell工具
    插入、删除和随机查询时间复杂度都为O(1) leetcode 381
    组合总和3 leetcode 216
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10193884.html
Copyright © 2011-2022 走看看