java线程生产者与消费者实例(使用List实现同步)

学习java线程的时候,看到生产者与消费者例子,有感而发。

下面是模拟汽车生产商和顾客(权当消费者可以直接从厂家买到汽车),废话不说,上代码。

package com.zx.thread.work;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * 我们模拟一个生产者和消费者购买汽车的例子
 * 
 * @author maping
 * 
 */
public class ProducerAndConsumer {
    //厂家存放汽车的车间
    private static List<Car> cars = new ArrayList<Car>();

    static void test(int producers, int consumers) {
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < producers; i++) {
            // 上海大众的producers个分厂分别生产了100台polo车
            exec.execute(new CarProducer("上海大众", cars, 100, i));
        }

        for (int i = 0; i < consumers; i++) {
            // 消费者张三的consumers亲戚分别买10台polo
            exec.execute(new CarConsumer("张三", cars, 10, i));
        }

        exec.shutdown();
    }

    public static void main(String[] args) throws FileNotFoundException {
        test(100, 1000);
    }
}

/**
汽车类

*/
class Car {
    private String name = "polo";

    private int id;

    private CarProducer producer;

    public Car() {
    }

    public Car(CarProducer producer, String name, int id) {
        this.producer = producer;
        this.name = name;
        this.id = id;
    }

    public String toString() {
        return this.producer.getName() + "#" + this.producer.getId() + "#"
                + this.getClass().getSimpleName() + "#" + name + "#" + id;
    }
}


/*

汽车制造厂商
*/
class CarProducer implements Runnable {
    private int count;

    private List<Car> cars;

    private String name;

    private int id;

    public CarProducer(String name, List<Car> cars, int count, int id) {
        this.name = name;
        this.cars = cars;
        this.count = count;
        this.id = id;
    }

    public void addCar(int ids) {
        System.out.println(this.name + "#" + id + ">>开始生产汽车" + ids);
        //此处同步cars,如果使用BlockingQueue,则无需同步代码
        synchronized (cars) {
            cars.add(new Car(this, name, ids));
            //车间里面有了刚刚制造好的汽车,那么就可以通知消费者来买了
            cars.notifyAll();
        }

    }

    public void run() {
        for (int i = 0; i < count; i++) {
            this.addCar(i);
        }
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}


/**
消费者
*/
class CarConsumer implements Runnable {
    private List<Car> cars;

    private int count;

    private String name;

    private int id;

    public CarConsumer(String name, List<Car> cars, int count, int id) {
        this.name = name;
        this.cars = cars;
        this.count = count;
        this.id = id;
    }

    public void buyCar() throws InterruptedException {
     //消费者购买汽车时,锁定汽车对象
        synchronized (cars) {
            System.out.println(name + "#" + id + ">>" + "开始选车");
            int waitTime = 10 * 1000;
          //消费者刚刚来到汽车存放的车间时,如果发现
          //车间还没有汽车,那么他只能等待,这里设置了超时时间,如果
         //超过指定时间,则一气走之,相当于抛出异常了
            while (cars.isEmpty()) {
                System.out.println("[" + name + "#" + id + "]当前车缺货,等待"
                        + waitTime + "毫秒");
                wait(waitTime);
            }
         //有汽车来了,则个时候就可以买汽车了
            Car car = cars.remove(0);
            System.out.println("[" + name + "#" + id + "]" + "买到车了" + car);
            System.out.println("***************************");
            System.err.println("当前库存为\n" + cars.size());
        }
    }

    public void run() {
        while (count-- > 0) {
            try {
                buyCar();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

这段代码在生产者线程制造的汽车数>=消费者线程购买的汽车数时,没有任何问题,当反之的时候,就会由于

等待制造商生产汽车,而此时制造商或许不再制造汽车了,那么就会抛出异常。

    我在思考,比如MQ的消息队列是怎么做到那么高效率呢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值