生产消费互相竞争,最多生产20个
生产一个消费一个
本文地址:https://liuyanzhao.com/7757.html
- package thread.test;
- /**
- * @author 言曌
- * @date 2018/3/14 上午9:19
- */
- public class ProductTest {
- public static void main(String args[]) {
- Product product = new Product();
- new Thread(new Producer(product)).start();
- new Thread(new Consumer(product)).start();
- }
- static class Product {
- private static final int MAX_PRODUCT = 20;
- private static final int MIN_PRODUCT = 0;
- private int PRODUCT = 0;
- synchronized public void add() {
- if (this.PRODUCT >= MAX_PRODUCT) {
- try {
- System.out.println("产品已满,请稍后生产");
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return;
- }
- this.PRODUCT++;
- System.out.println("生产者生产了第" + this.PRODUCT + "个产品" + ",时间:" + System.currentTimeMillis());
- notifyAll();
- }
- synchronized public void sub() {
- if (this.PRODUCT <= MIN_PRODUCT) {
- try {
- System.out.println("产品处于缺货状态");
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return;
- }
- System.out.println("消费者消费了第" + this.PRODUCT + "个产品" + ",时间:" + System.currentTimeMillis());
- this.PRODUCT--;
- notifyAll();
- }
- }
- static class Producer implements Runnable {
- private Product product;
- public Producer(Product product) {
- this.product = product;
- }
- @Override
- public void run() {
- System.out.println("生产者开始生产产品");
- while (true) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- product.add();
- }
- }
- }
- static class Consumer implements Runnable {
- private Product product;
- public Consumer(Product product) {
- this.product = product;
- }
- @Override
- public void run() {
- System.out.println("消费者开始消费产品");
- while (true) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- product.sub();
- }
- }
- }
- }
生产一个消费一个
- package thread.test;
- /**
- * @author 言曌
- * @date 2018/3/14 上午9:19
- */
- public class ProductTest2 {
- public static void main(String args[]) {
- Product product = new Product();
- new Thread(new Producer(product)).start();
- new Thread(new Consumer(product)).start();
- }
- static class Product {
- volatile private boolean isEmpty = true;
- synchronized public void add() {
- while (!isEmpty) {
- try {
- System.out.println("已经有了产品,等待消费");
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println("生产者生产了一个产品,时间" + System.currentTimeMillis());
- isEmpty = false;
- notify();
- }
- synchronized public void sub() {
- while (isEmpty) {
- try {
- System.out.println("没有了产品,等待生产");
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println("消费者消费了一个产品,时间" + System.currentTimeMillis());
- isEmpty = true;
- notify();
- }
- }
- static class Producer implements Runnable {
- private Product product;
- public Producer(Product product) {
- this.product = product;
- }
- @Override
- synchronized public void run() {
- while (true) {
- try {
- Thread.sleep(1);//强制切换线程
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- product.add();//添加一个商品
- }
- }
- }
- static class Consumer implements Runnable {
- private Product product;
- public Consumer(Product product) {
- this.product = product;
- }
- @Override
- synchronized public void run() {
- while (true) {
- try {
- Thread.sleep(1);//强制切换线程
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- product.sub();//减少一个商品
- }
- }
- }
- }
本文地址:https://liuyanzhao.com/7757.html
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏