程序清单
Posted guchunchao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了程序清单相关的知识,希望对你有一定的参考价值。
程序清单 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(); } } }
以上是关于程序清单的主要内容,如果未能解决你的问题,请参考以下文章