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 <= 0) {
wait();
}
String goods = buf[head];
head = (head + 1) % buf.length;
count--;
notifyAll();
System.out.println(Thread.currentThread().getName() + " get " + goods);
return goods;
}
}
// 生产者线程
class ProducerThread extends Thread {
private final Container container;
private static int id = 0;
public ProducerThread(String threadName, Container container) {
super(threadName);
this.container = container;
}
@Override
public void run() {
try {
while(true) {
Thread.sleep(1000);
String goods = "[goods No." + nextID() + " by " + getName() + "]";
this.container.put(goods);
}
} catch (InterruptedException e) {}
}
private static synchronized int nextID() {
return id++;
}
}
// 消费者线程
class ConsumerThread extends Thread {
private final Container container;
public ConsumerThread(String threadName, Container container) {
super(threadName);
this.container = container;
}
@Override
public void run() {
try {
while(true) {
String goods = this.container.get();
Thread.sleep(2000);
}
} catch (InterruptedException e) {}
}
}
public class Main {
public static void main(String[] args) {
Container container = new Container(3);
new ProducerThread("ProducerThread-1", container).start();
new ProducerThread("ProducerThread-2", container).start();
new ProducerThread("ProducerThread-3", container).start();
new ConsumerThread("ConsumerThread-1", container).start();
new ConsumerThread("ConsumerThread-2", container).start();
new ConsumerThread("ConsumerThread-3", container).start();
}
}
用阻塞队列BlockingQueue模拟生产者消费者
结果:
A_a.txt
B_a.txt
C.java
A_b.txt
发现个问题,递归 + 并发 文件顺序乱了?
public class ProducerConsumer {
static class FileCrawler implements Runnable {
private final BlockingQueue<File> fileQueue;
private final FileFilter fileFilter;
private final File root;
public FileCrawler(BlockingQueue<File> 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<File> queue;
public Indexer(BlockingQueue<File> 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<File> queue = new LinkedBlockingQueue<File>(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 < N_CONSUMERS; i++)
new Thread(new Indexer(queue)).start();
}
public static void main(String[] args) {
ProducerConsumer.startIndexing(new File[] {new File("D:/test")});
}
}