感觉还有点小问题 , 大家帮忙看看
import java.util.ArrayList;
public class Generant_Customer {
public static void main(String[] args) {
Collection c = new Collection();
Generant gen = new Generant(c);
Customer cus = new Customer(c);
new Thread(gen,"gen01").start();
new Thread(gen,"gen02").start();
new Thread(gen,"gen03").start();
new Thread(cus,"cus01").start();
new Thread(cus,"cus02").start();
}
}
/**
* 资源类 , 消费者和生产者共同操作的资源
*/
class Collection {
ArrayList<String> collection = new ArrayList<String>();
static final int length = 10;
int index = 1 ;
int operateCount = 0;
/* 生产者生产 */
public synchronized void add(){
if(collection.size()>=length){
try {
wait();
} catch (InterruptedException e) {
System.out.println("add() has been interrupted !");
System.exit(1);
}
}
notifyAll();//唤醒在此对象监视器上等待的单个线程
collection.add("馒头 编号:"+index);
System.out.println("生产者" +Thread.currentThread().getName() + collection.get(index-1));
index ++ ;
operateCount++;
}
/* 消费者消费 */
public synchronized void pop(){
if(collection.size() == 0){
try {
wait();
} catch (InterruptedException e) {
System.out.println("pop() has been interrupted !");
System.exit(1);
}
}
notifyAll(); //唤醒在此对象监视器上等待的单个线程
System.out.println("--消费者" +Thread.currentThread().getName() + collection.get(collection.size()-1));
collection.remove(collection.size()-1);
index -- ;
operateCount++;
}
}
class Generant implements Runnable{
Collection coll = null ;
public Generant(Collection collection ){
this.coll = collection ;
}
/* 生产者开始执行 */
public void run() {
while(true){
coll.add();
System.out.println(coll.operateCount);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("Generant.run() has been interrupted !\\n" +
" System continue...");
}
}
}
}
class Customer implements Runnable{
Collection coll = null ;
/* 线程开关 */
boolean doRun = true ;
public Customer(Collection collection ){
this.coll = collection ;
}
/* 消费者开始执行 */
public void run() {
while(doRun){
coll.pop();
System.out.println(coll.operateCount);
try {
Thread.sleep(150);
} catch (InterruptedException e) {
System.out.println("Customer.run() has been interrupted !\\n" +
" System continue...");
}
if(coll.operateCount == 50){
doRun = false;
}
}
}
}
分享到:
相关推荐
使用信号量实现有限缓冲区的生产者和消费者问题 使用信号量实现读进程具有优先权的读者和写者问题
生产者和消费者问题以及哲学家就餐问题,JAVA实现的程序.rar产者和消费者问题以及哲学家就餐问题,JAVA实现的程序.rar
模拟PV操作同步机构,且用PV操作解决生产者——消费者问题。
使用信号量实现有限缓冲区的生产者和消费者问题 使用信号量实现读进程具有优先权的读者和写者问题 实验报告(内容、环境、遇到的问题及解决、源代码、流程图、总结) 源代码
实验一 生产者和消费者问题 因为源码比较长,所以想在网上找现成的,不想自己写,结果找了好久,发现:第一,《操作系统实验指导》基本上找不到电子版的;第二,这个实验的源码也很难找(反正我是没找到,好不容易...
进程同步模拟设计--生产者和消费者问题 进程调度同步异步
操作系统生产者和消费者问题操作系统生产者和消费者问题操作系统生产者和消费者问题操作系统生产者和消费者问题操作系统生产者和消费者问题
(1)创建生产者和消费者线程 在Windows2000环境下,创建一个控制台进程,在此进程中创建n个线程来模拟生产者或者消费者。这些线程的信息由本程序定义的“测试用例文件”中予以指定。 该文件的格式和含义如下: 3 1 ...
linux下C++实现生产者和消费者问题,并及时输出缓存区的状态,方便理解。代码中有详细的注释,方便阅读。
操作系统经典实验,模拟生产者消费者问题,采用java语言编写,互斥同步
(1)掌握基本的同步互斥算法,理解生产者和消费者同步的问题模型。 (2)了解Windows 2000/XP中多线程的并发执行机制,线程间的同步和互斥。 (3)学习使用Windows2000/XP中基本的同步对象,掌握相应的API。 2、...
四川大学操作系统课程设计高分报告-第三次实验-生产者和消费者问题.doc 都是自己非常认真完成的,每一个要点都实现到位,程序全部跑通且符合要求。 保证每题均正确。 最后得到的分数也很好(均为最高分)
VC中实现的生产者和消费者问题的算法。此为转帖,算法讲的不错。
课程设计A:生产者和消费者问题课程设计A:生产者和消费者问题课程设计A:生产者和消费者问题
用java模拟生产者和消费者在缓冲区间的阻塞情况。
操作系统线程同步和互斥,生产者和消费者问题例子,VC++编写
进程中创建多个线程模拟生产者和消费者,并且满足以下要求:生产者可以把产品放到任意缓冲区中,消费者只消费指定生产者的产品,且为各生产者分配缓冲区时,各生产者必须互斥,各生产者的具体活动并发,而消费者只有...
C语言编程模拟生产者和消费者问题附代码程序.doc
操作系统生产者和消费者问题.doc
当生产者向缓冲区放入了一个数据,应唤醒正在等待的消费者进程,同样,当消费者取走一个数据后,应唤醒正在等待的生产者进程。就是生产者和消费者之间的同步。 每次写入和读出数据时,都将读和写指针加一。当读写...