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-->通知木头工厂生产木头

如有问题,请联系我

你可能感兴趣的:(多线程)