码迷,mamicode.com
首页 > 其他好文 > 详细

程序清单

时间:2019-04-05 14:21:17      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:lin   tor   trie   directory   getname   应用   nal   new   ali   

 

 

程序清单 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();
        }
    }

}

 


 

程序清单

标签:lin   tor   trie   directory   getname   应用   nal   new   ali   

原文地址:https://www.cnblogs.com/guchunchao/p/10658372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!