生产者消费者模型实现<一>模拟实现

本文详细介绍了多线程编程中生产者消费者模型的应用,并通过Store类、Producer类、Consumer类实现了模拟。展示了如何利用同步机制(synchronized关键字)确保线程安全,以及如何在不同生产者消费者比例下的工作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

熟悉生产者消费者模型是学习多线程的编程的必经之路,它广泛应用于各种系统中,如TCP消息队列等。下面是模拟实现的代码(为什么叫模拟,请看<二>真实实现就懂了)。


Store.java

package test.producerAndConsumer1;

/**
 * 仓库类
 * create by qiuping.wu on 2015-08-10
 */
public class Store {
	private int count = 0;
	private final int MAX_NUM = 10;

	public void Produce() {
		while (count >= MAX_NUM) {
			System.out.println(Thread.currentThread().getName()+"FFFFFFFFFFFFFFFFFFFFFFFF队列满");
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		synchronized (this) {
			count++;
			System.out.println(Thread.currentThread().getName()+"++++++++++生产者往仓库放入一件商品,count=" + count);
		}
	}

	public void Consume() {
		while (count <= 0) {
			System.out.println(Thread.currentThread().getName()+"00000000000队列空");
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		synchronized (this) {
			count--;
			System.out.println(Thread.currentThread().getName()+"---------------消费者取出一件商品,count=" + count);
		}
	}

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		Store store  = new Store();
		store.Produce();
		store.Produce();
		store.Consume();
		store.Consume();
		store.Consume();
	}

}



Producer.java

package test.producerAndConsumer1;

/**
 * 生产者类
 * create by qiuping.wu on 2015-08-10
 */
public class Producer implements Runnable {
private Store store;
	public Producer(Store store) {
		this.store = store;
	}
	
	@Override
	public void run()
	{
		while(true)
		{
			store.Produce();
			
			try {
				Thread.sleep(900);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}

Consumer.java

package test.producerAndConsumer1;

/**
 * 消费者类
 * create by qiuping.wu on 2015-08-10
 */
public class Consumer implements Runnable {
	private Store store;

	public Consumer(Store store) {
		this.store = store;
	}

	@Override
	public void run() {
		while (true) {
			store.Consume();

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

测试一:生产者:消费者= 1:1


testSingle.java

package test.producerAndConsumer1;

/**
 * 单生产者单消费者测试类
 * create by qiuping.wu on 2015-08-10
 */
public class testSingle {

	public static void main(String[] args) {
		Store store  = new Store();
		Producer p = new Producer(store);
		Consumer c  = new Consumer(store);
		new Thread(p).start();
		new Thread(c).start();
	}

}


测试二:生产者:消费者= m:n


testMulti.java

package test.producerAndConsumer1;

/**
 * 多生产者多消费者测试类
 * create by qiuping.wu on 2015-08-10
 */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class testMulti {

	public static void main(String[] args) {
		ExecutorService es = Executors.newCachedThreadPool();

		Store store = new Store();
		Producer p = new Producer(store);
		Consumer c = new Consumer(store);

		for (int i = 0; i < 3; i++) {
			es.submit(p);
		}
		for (int i = 0; i < 4; i++) {
			es.submit(c);
		}

	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值