java-多线程--模拟生产者和消费者

本例中,生产者生产木头之后通知供应商消费木头,供应商消费完了木头就会通知木头工厂生产木头

生产者代码如下:

/**
 * @Project: 生产者
 * @Author: liming
 * @Date: 2018年06月04日
 */
package com.withmes.demo.thread;

import java.util.Random;

/**
 * ClassName: Product
 *
 * @author liming
 * @Description: 生产者
 * @date 2018年06月04日
 */
public class Product extends Thread {
    private WoodFactor woodFactor;
    private String ClassName = this.getClass().getName() + "-->";

    Product(WoodFactor woodFactor) {
        this.woodFactor = woodFactor;
    }

    public void productWood() {
        try {
            synchronized (woodFactor) {
                Thread.sleep(1000);
                if (woodFactor.getRemmainWodd() > 0) {
                    System.out.println(ClassName + "木头工厂有充足的木头,不需要再生产多的木头了...等待供应商消费...");
                    woodFactor.wait();
                }
                Random random = new Random();
                int anInt = random.nextInt(10);
                System.out.println(ClassName + "木头工厂生产了" + anInt + "个木头!");
                woodFactor.setRemmainWodd(anInt);
                System.out.println(ClassName + "通知所有供应商消费...");
                woodFactor.notify();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        super.run();
        while (true) {
            productWood();
        }
    }
}

消费者代码如下:

/**
 * @Project: 供应商
 * @Author: liming
 * @Date: 2018年06月04日
 */
package com.withmes.demo.thread;

/**
 * ClassName: Consump
 *
 * @author liming
 * @Description: 供应商
 * @date 2018年06月04日
 */
public class Consume extends Thread {
    private WoodFactor woodFactor;
    private String ClassName = this.getClass().getName() + "-->";


    Consume(WoodFactor woodFactor) {
        this.woodFactor = woodFactor;
    }

    public void consumeWood() {
        try {
            synchronized (woodFactor) {
                Thread.sleep(1000);
                if (woodFactor.getRemmainWodd() <= 0) {
                    System.out.println(ClassName + "供应商发现没有木头了,通知木头工厂生产木头");
                    woodFactor.wait();
                }
                //模拟消费木头
                Integer remainWood = woodFactor.getRemmainWodd();
                woodFactor.setRemmainWodd(0);
                System.out.println(ClassName + "供应商一共消费了" + remainWood + "个木头");
                //通知木头工厂
                woodFactor.notify();
                System.out.println(ClassName + "通知木头工厂生产木头");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        super.run();
        while (true) {
            consumeWood();
        }
    }
}

测试类:

/**
 * @Project: 测试
 * @Author: liming
 * @Date: 2018年06月04日
 */
package com.withmes.demo.thread;

/**
 * ClassName: TestProductConsume
 * @Description: 测试
 * @author liming
 * @date 2018年06月04日
 */
public class TestProductConsume {
    public static void main(String[] args) {
        WoodFactor woodFactor = new WoodFactor();
        Product product = new Product(woodFactor);
        Consume consume
                 = new Consume(woodFactor);
        product.start();
        consume.start();
    }
}

控制台预期结果:

com.withmes.demo.thread.Product-->木头工厂生产了5个木头!
com.withmes.demo.thread.Product-->通知所有供应商消费...
com.withmes.demo.thread.Consume-->供应商一共消费了5个木头
com.withmes.demo.thread.Consume-->通知木头工厂生产木头
com.withmes.demo.thread.Consume-->供应商发现没有木头了,通知木头工厂生产木头
com.withmes.demo.thread.Product-->木头工厂生产了5个木头!
com.withmes.demo.thread.Product-->通知所有供应商消费...
com.withmes.demo.thread.Product-->木头工厂有充足的木头,不需要再生产多的木头了...等待供应商消费...
com.withmes.demo.thread.Consume-->供应商一共消费了5个木头
com.withmes.demo.thread.Consume-->通知木头工厂生产木头
com.withmes.demo.thread.Consume-->供应商发现没有木头了,通知木头工厂生产木头
com.withmes.demo.thread.Product-->木头工厂生产了4个木头!
com.withmes.demo.thread.Product-->通知所有供应商消费...
com.withmes.demo.thread.Product-->木头工厂有充足的木头,不需要再生产多的木头了...等待供应商消费...
com.withmes.demo.thread.Consume-->供应商一共消费了4个木头
com.withmes.demo.thread.Consume-->通知木头工厂生产木头
com.withmes.demo.thread.Consume-->供应商发现没有木头了,通知木头工厂生产木头
com.withmes.demo.thread.Product-->木头工厂生产了7个木头!
com.withmes.demo.thread.Product-->通知所有供应商消费...
com.withmes.demo.thread.Product-->木头工厂有充足的木头,不需要再生产多的木头了...等待供应商消费...
com.withmes.demo.thread.Consume-->供应商一共消费了7个木头
com.withmes.demo.thread.Consume-->通知木头工厂生产木头
com.withmes.demo.thread.Consume-->供应商发现没有木头了,通知木头工厂生产木头
com.withmes.demo.thread.Product-->木头工厂生产了4个木头!
com.withmes.demo.thread.Product-->通知所有供应商消费...
com.withmes.demo.thread.Product-->木头工厂有充足的木头,不需要再生产多的木头了...等待供应商消费...
com.withmes.demo.thread.Consume-->供应商一共消费了4个木头
com.withmes.demo.thread.Consume-->通知木头工厂生产木头

如有问题,请联系我

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值