生产者/消费者实现方式一: 管程法

package com.yunding.concurrent;

/**
 * 生产者/消费者实现方式一: 管程法
 * 
 * @author beOkWithAnything
 *
 */
public class Test {

	public static void main(String[] args) {
		Container container = new Container();
		new Productor(container).start();
		new Consumer(container).start();
	}

}

// 生产者
class Productor extends Thread {
	// 持有对产品容器的引用
	Container container;

	public Productor(Container container) {
		this.container = container;
	}

	@Override
	public void run() {
		// 生产
		for (int i = 0; i < 50; i++) {
			container.produce(new Product(i));
		}
	}
}

// 消费者
class Consumer extends Thread {
	// 持有对产品容器的引用
	Container container;

	public Consumer(Container container) {
		this.container = container;
	}

	@Override
	public void run() {
		// 消费
		for (int i = 0; i < 50; i++) {
			container.getProduct();
		}
	}
}

// 缓冲区
class Container {
	// 计数器
	int count = 0;
	// 存储容器
	Product[] products = new Product[10];

	// 生产产品
	synchronized void produce(Product product) {
		// 缓冲区已经填满,不能再生产
		if (count == products.length) {
			try {
				// 线程阻塞
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// 生产了产品
		products[count] = product;
		count++;
		System.out.println("生产-- 》 第" + product.id + "个产品");
		// 通知消费者可以购买了
		this.notifyAll();
	}

	// 获取产品
	synchronized Product getProduct() {
		// 如果没有产品
		if (count == 0) {
			try {
				// 线程阻塞
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// 消费了
		count--;
		Product product = products[count];
		System.out.println("消费-- 》 第" + product.id + "个产品");
		// 通知生产者可以开始生产了
		this.notifyAll();
		return product;
	}
}

// 产品
class Product {
	int id;

	public Product(int id) {
		this.id = id;
	}

}

 

全部评论

相关推荐

08-08 16:33
唐山学院 Java
职场水母:首先,简历太长,对于实习和应届找工作,hr一眼扫的是学历,技术看实习,你写的技术栈字太多了,尽量用一句话概括不用写那么详细,技术面的时候会问的,而且技术栈都会在实习或者项目里体现,你要做的是,把你的简历浓缩为一页,删除没用的东西,比如实践经历,自我评价,这些纯废话,没用,专业技能写的太离谱,你真的熟练掌握了吗,建议都写熟悉,找工作和写论文不一样,追求的是干练和实用,把实习经历和项目提前,把掌握的技术栈写到最后,然后去找实习,
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务