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及更高版本提供的线程库()、互斥量()、条件变量()等...
7. **例程分析**:在提供的"生产者消费者"例程中,可能包含了创建生产者和消费者线程、初始化队列、添加数据到队列、从队列中取出数据、以及使用同步机制保证正确性的代码片段。通过对这些例程的分析和运行,可以...
"生产者消费者问题C++代码实现" 生产者消费者问题是一个经典的进程同步问题,该问题最早由 Dijkstra 提出,用以演示他提出的信号量机制。在同一个进程地址空间内执行的两个线程。生产者线程生产物品,然后将物品...
生产者消费者问题总结 生产者消费者问题是操作系统中的一种经典问题,它描述了在多进程或多线程环境中,生产者和消费者之间的同步问题。在这个问题中,生产者负责生产数据,而消费者则负责消费这些数据。为了解决这...
生产者消费者问题是多线程编程中的一个经典案例,它展示了如何通过线程间的协作来解决资源的同步和异步操作。在C++中,我们可以利用标准库中的互斥量(mutex)、条件变量(condition_variable)等工具来实现这个问题...
在IT领域,生产者消费者架构是一种常见的多线程或并发编程模型,用于高效地处理数据流。这个模型基于消息队列的概念,适用于各种环境,包括LabVIEW(Laboratory Virtual Instrument Engineering Workbench)这样的...
生产者消费者问题是多线程编程中的经典模型,用于展示如何高效地在多个线程之间共享资源。MFC(Microsoft Foundation Classes)是微软提供的一套面向对象的C++库,用于构建Windows应用程序。在这个问题中,我们将...
生产者消费者模式是一种经典的多线程同步问题解决方案,它源于现实世界中的生产流水线,用于描述生产者(Producer)和消费者(Consumer)之间的协作关系。在这个模式中,生产者负责生成产品并放入仓库,而消费者则从...
生产者消费者问题是多线程编程中的一个经典案例,它展示了如何通过线程间的协作来解决资源的并发访问问题。在C#中,我们可以利用System.Threading命名空间提供的工具来实现这一模型。下面将详细阐述这个问题的背景、...
生产者消费者问题是多线程编程中的经典模型,用于模拟两个或多个并发执行的实体(生产者和消费者)共享有限资源的情况。在这个问题中,生产者负责生成数据并放入缓冲区,而消费者则从缓冲区取出数据进行处理。当缓冲...
多线程实现生产者消费者模型:锁(Lock)、信号量(Semaphore、BoundedSemaphore)、条件(Condition)、队列(Queue)、事件(Event) 多进程程实现生产者消费者模型:信号量(Semaphore)、条件(Condition)、...
生产者消费者问题是多线程编程中的一个经典案例,它展示了如何通过共享资源在并发环境中实现线程间的协调。在这个问题中,"生产者"线程负责生成数据,而"消费者"线程则负责消费这些数据。MFC(Microsoft Foundation ...
**Qt入门练习项目——生产者消费者模型** 在编程领域,生产者消费者模型是一种常见的多线程同步问题的解决方案。这个模型通常用于处理数据流的异步处理,其中一个或多个线程(生产者)生成数据,而其他线程(消费者...
操作系统中的“生产者消费者”问题是一个经典的多线程同步问题,源自计算机科学的并发控制理论。这个模型描述了两个或多个线程之间的交互,其中一部分线程(生产者)负责生成数据,另一部分线程(消费者)则负责处理...
设计目的:通过研究Linux 的进程机制和信号量实现生产者消费者问题的并发控制。说明:有界缓冲区内设有20 个存储单元,放入/取出的数据项设定为1‐20 这20 个整型数。设计要求:1)每个生产者和消费者对有界缓冲区...
C语言实现生产者消费者问题,分配具有n个缓冲区的缓冲池,作为共享资源。 定义两个资源型信号量empty 和full,empty信号量表示当前空的缓冲区数量,full表示当前满的缓冲区数量。 定义互斥信号量mutex,当某个进程...