多线程之生产者与消费者简单实例思路与源码

讲到多线程基础
生产者和消费者就理应拿出来说道说道

生产者与消费者的大致思路如下。
1.当库存为空的时候,消费者进入等待状态,生产者进行生产工作。
2.当库存满的时候,生产者进入等待状态,消费者进行消费。
3.当库存不为空,又不为满的时候,二者争用对象锁,拿锁者,拿天下。

因此,代码思路出来了。
首先定义库存峰值

// 库存峰值
	private final static int max = 10;

其次定义当前库存量(初始值为0)

// 当前库存量
	private int num = 0;

然后定义锁对象

// 锁对象
	private Object obj = new Object();

定义完毕

接下来就是定义生产者和消费者的工作模式了

/**
	 * 生产者
	 * 
	 * @param name
	 */
	public void producer(String name) {
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) { // 同步锁
					synchronized (obj) {
						if (num < max) {
							System.out.println("生产者:" + name + "  " + "正在生产第:" + (num + 1 )+ "个");
							num++;
							System.out.println("当前库存:NUM=" + num);
							// 睡眠1秒
							try {
								Thread.currentThread().sleep(1000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							// 线程唤醒
							obj.notify();
						} else {
							System.out.println("库存已满...生产者等待中...");
							try {
								obj.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}
				}
			}
		});

		t.start();
	}

	/**
	 * 消费者
	 * 
	 * @param name
	 */
	public void customer(String name) {
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					synchronized (obj) {
						if (num > 0) {
							System.out.println("消费者: " + name + " " + "正在消费第:" + num + "个");
							num--;
							System.out.println("当前库存:NUM=" + num);
							try {
								Thread.currentThread().sleep(1000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							obj.notify();
						} else {
							System.out.println("仓库已空..正在等待生产...");
							try {
								obj.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		});

		t.start();
	}

思路如上所示。

下面是完整代码与测试

package threadtest;

public class ProducerAndCustomer {

	// 库存峰值
	private final static int max = 10;
	// 当前库存量
	private int num = 0;
	// 锁对象
	private Object obj = new Object();

	/**
	 * 生产者
	 * 
	 * @param name
	 */
	public void producer(String name) {
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) { // 同步锁
					synchronized (obj) {
						if (num < max) {
							System.out.println("生产者:" + name + "  " + "正在生产第:" + (num + 1 )+ "个");
							num++;
							System.out.println("当前库存:NUM=" + num);
							// 睡眠1秒
							try {
								Thread.currentThread().sleep(1000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							// 线程唤醒
							obj.notify();
						} else {
							System.out.println("库存已满...生产者等待中...");
							try {
								obj.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}
				}
			}
		});

		t.start();
	}

	/**
	 * 消费者
	 * 
	 * @param name
	 */
	public void customer(String name) {
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					synchronized (obj) {
						if (num > 0) {
							System.out.println("消费者: " + name + " " + "正在消费第:" + num + "个");
							num--;
							System.out.println("当前库存:NUM=" + num);
							try {
								Thread.currentThread().sleep(1000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							obj.notify();
						} else {
							System.out.println("仓库已空..正在等待生产...");
							try {
								obj.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		});

		t.start();
	}

}

测试类

package threadtest;

public class MoreThread{

	public static void main(String[] args) {
		ProducerAndCustomer pac=new ProducerAndCustomer();
		pac.producer("Producer A");
		pac.producer("Producer B");
		pac.producer("Producer C");
		pac.customer("Customer 1");
		pac.customer("Customer 2");
		pac.customer("Customer 3");
		pac.customer("Customer 4");
	}
}

测试结果:
在这里插入图片描述测试没问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值