zoukankan      html  css  js  c++  java
  • 程序清单

    程序清单 5-8 桌面搜索应用程序中的生产者任务和消费者任务(和书上有所不同,有些许改动)


    1、生产者

    package com.everjiankang.miaosha;
    
    import java.io.File;
    import java.io.FileFilter;
    import java.util.Set;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ConcurrentSkipListSet;
    
    /**
     * 文件爬虫程序
     * @author guchunchao
     *
     */
    public class FileCrawer implements Runnable {
    
        private final BlockingQueue<File> fileQueue;
        private final FileFilter fileFilter;
        private final File root;
        private final Set<String> alreadyExistFileSet = new ConcurrentSkipListSet<>();
            
        public FileCrawer(BlockingQueue<File> fileQueue, FileFilter fileFilter, File root) {
            super();
            this.fileQueue = fileQueue;
            this.fileFilter = fileFilter;
            this.root = root;
        }
    
    
        @Override
        public void run() {
            try {
                this.crawl(root);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
    
        }
        
        
        private void crawl(File root) throws InterruptedException {
            if(root != null) {
                File[] entries = root.listFiles(fileFilter);
                if(entries != null) {
                    for(File file : entries) {
                        if(file.isDirectory()) 
                            crawl(file);
                        else if(!alreadyIndexed(file)) {
                            System.out.println("生产者放入队列:" + file.getPath() + file.getName());
                            fileQueue.put(file);
                            alreadyExistFileSet.add(file.getPath()+file.getName());
                        }
                    }
                } 
            }
        }
        
        private boolean alreadyIndexed(File file) {
            return alreadyExistFileSet.contains(file.getPath()+file.getName());
    //        return fileQueue.contains(file);
        }
    
    }

    2、消费者

    package com.everjiankang.miaosha;
    
    import java.io.File;
    import java.util.concurrent.BlockingQueue;
    
    /**
     * 消费者,建立文件索引
     * @author guchunchao
     *
     */
    public class Indexer implements Runnable {
        
        private final BlockingQueue<File> queue;
        
        public Indexer(BlockingQueue<File> queue) {
            this.queue = queue;
        }
    
        @Override
        public void run() {
            try {
                while(true) {
                    indexFile(queue.take());
                }
            } catch(InterruptedException e) {
                Thread.currentThread().interrupt();
            }
    
        }
    
        private void indexFile(File file) {
            System.out.println("消费:" + file.getPath()+file.getName());
        }
    
    }

    3调用

    package com.everjiankang.miaosha;
    
    import java.io.File;
    import java.io.FileFilter;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingDeque;
    
    public class XiaoFeiQueue {
        private final static int BOUND = 10;
        private final static int N_CONSUMMERS = 10;
    
        public static void main(String[] args) {
            File file = new File("/Users/guchunchao/Desktop/深入理解java虚拟机视频1-6");
            File[] files = new File[10];
            files[0] = file;
            startIndexing(files);
    
        }
        
        public static void startIndexing(File[] files) {
            BlockingQueue<File> queue = new LinkedBlockingDeque<File>(BOUND);
            FileFilter filter = new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return true;
                }
            };
            
            //n个线程爬取文件
            for(File file : files) {
                new Thread(new FileCrawer(queue, filter, file)).start();
            }
            
            //10个线程消费
            for(int i = 0; i < N_CONSUMMERS; i++) {
                new Thread(new Indexer(queue)).start();
            }
        }
    
    }

    程序清单 5-11 在计测试中使用CountDownLatch来启动和停止线程


    public static void say(int nThread, Runnable run) throws InterruptedException {
            CountDownLatch startDoor =new CountDownLatch(1);
            CountDownLatch endDoor = new CountDownLatch(nThread);
            for(int i = 0; i < nThread; i++) {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            startDoor.await();
                            try {
                                run.run();
                            }finally {
                                endDoor.countDown();
                            }
                        } catch (InterruptedException e) {
                        }
                    }
                }.start();
            }
            
            long startTime = System.nanoTime();
            startDoor.countDown();
            endDoor.await();
            long endTime = System.nanoTime();
            System.out.println(nThread + "个线程总共花费了" + (endTime - startTime) + " ns");
    }
  • 相关阅读:
    java里的分支语句--程序运行流程的分类(顺序结构,分支结构,循环结构)
    Java里的构造函数(构造方法)
    Java里this的作用和用法
    JAVA中的重载和重写
    从键盘接收字符类型的数据并实现剪刀石头布的规则
    使用Notepad++编码编译时报错(已解决?)
    云就是网络,云计算呢
    使用JavaMail创建邮件和发送邮件
    mysql锁机制
    java中几种常用的设计模式
  • 原文地址:https://www.cnblogs.com/guchunchao/p/10658372.html
Copyright © 2011-2022 走看看