生产者消费者

生产者消费者
2011年07月01日
   package com.apq.producer_consumer; //装东西的容器 class Container { private final String[] buf; private int tail; private int head; private int count; public Container(int maxBufferSize) { this.buf = new String[maxBufferSize]; this.tail = 0; this.head = 0; this.count = 0; } public synchronized void put(String goods) throws InterruptedException { System.out.println(Thread.currentThread().getName( ) + " put " + goods); while(count >= buf.length) { wait(); } buf[tail] = goods; tail = (tail + 1) % buf.length; count++; notifyAll(); } public synchronized String get() throws InterruptedException { while(count fileQueue; private final FileFilter fileFilter; private final File root; public FileCrawler(BlockingQueue fileQueue, final FileFilter fileFilter, File root) { this.fileQueue = fileQueue; this.root = root; this.fileFilter = new FileFilter() { public boolean accept(File f) { return f.isDirectory() || fileFilter.accept(f); } }; } private boolean alreadyIndexed(File f) { return false; } public void run() { try { crawl(root); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } private void crawl(File root) throws InterruptedException { File[] entries = root.listFiles(fileFilter); if (entries != null) { for (File entry : entries) if (entry.isDirectory()) crawl(entry); else if (!alreadyIndexed(entry)) fileQueue.put(entry); } } } static class Indexer implements Runnable { private final BlockingQueue queue; public Indexer(BlockingQueue queue) { this.queue = queue; } public void run() { try { while (true) indexFile(queue.take()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } public void indexFile(File file) { // Index the file... System.out.println(file.getName()); }; } private static final int BOUND = 10; private static final int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); public static void startIndexing(File[] roots) { BlockingQueue queue = new LinkedBlockingQueue(BOUND); FileFilter filter = new FileFilter() { public boolean accept(File file) { return true; } }; for (File root : roots) new Thread(new FileCrawler(queue, filter, root)).start(); for (int i = 0; i
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值