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")});
}
}
分享到:
相关推荐
生产者消费者问题解决方案 生产者消费者问题是计算机科学中的一种经典问题,描述的是在多线程环境中,多个生产者线程和消费者线程之间的协作问题。生产者线程负责生产数据,并将其存储在缓冲区中,而消费者线程则从...
生产者消费者模式是一种多线程或并发编程中的经典设计模式,它主要用于解决系统资源的高效利用和同步问题。在C++中实现生产者消费者模式,我们可以利用C++11及更高版本提供的线程库()、互斥量()、条件变量()等...
在计算机科学中,"生产者消费者问题"是一个经典的并发编程模型,用于展示如何在多线程环境下有效地管理和共享资源。MFC(Microsoft Foundation Classes)是微软提供的一种C++库,用于构建Windows应用程序,它提供了...
7. **例程分析**:在提供的"生产者消费者"例程中,可能包含了创建生产者和消费者线程、初始化队列、添加数据到队列、从队列中取出数据、以及使用同步机制保证正确性的代码片段。通过对这些例程的分析和运行,可以...
"生产者消费者问题C++代码实现" 生产者消费者问题是一个经典的进程同步问题,该问题最早由 Dijkstra 提出,用以演示他提出的信号量机制。在同一个进程地址空间内执行的两个线程。生产者线程生产物品,然后将物品...