java实现多个生产者和消费者(传统方式与阻塞队列方式)

传统方式

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;

public class ProducerAndConsumer3 {
    public static void main(String[] args) {
        Clerk3 clerk3 = new Clerk3();
        for (int i = 0; i < 5; i++) {
            Prod3 p = new Prod3(clerk3);
            Thread tp = new Thread(p,"producer"+i);
            tp.start();
        }
        for (int i = 0; i < 20; i++) {
            Cons3 c = new Cons3(clerk3);
            Thread tc = new Thread(c,"consumer"+i);
            tc.start();
        }
    }
}

class Clerk3 {
    private AtomicInteger ai = new AtomicInteger();
    private Queue<String> queue = new LinkedList<>();
    public synchronized void addProduce() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (queue.size() >= 10) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            String data = ai.incrementAndGet()+"";
            queue.add(data);
            System.out.println("生产者线程"+Thread.currentThread().getName()+"生产了第"+data+"商品");
            notifyAll();
        }
    }
    public synchronized void getProduce() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (queue.size() <= 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            String data = queue.poll();
            System.out.println("消费者线程"+Thread.currentThread().getName()+"消费了第"+data+"商品");
            notifyAll();

        }
    }
}

class Prod3 implements Runnable{
    Clerk3 clerk;
    public Prod3(Clerk3 clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println("生产者开始生产产品");
        while (true) {
            try {
                Thread.sleep((int)Math.random() * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.addProduce();
        }
    }
}

class Cons3 implements Runnable{
    Clerk3 clerk;
    public Cons3(Clerk3 clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println("消费者开始消费产品");
        while (true) {
            try {
                Thread.sleep((int)Math.random() * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.getProduce();
        }
    }
}

阻塞队列

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class ProducerAndConsumerBlock3 {
    public static void main(String[] args) {
        Waiter waiter = new Waiter(new ArrayBlockingQueue(10));
        for (int i = 0; i < 5; i++) {
            CoffeeMarker cm = new CoffeeMarker(waiter);
            new Thread(cm,"生产线程"+i).start();
        }
        for (int i = 0; i < 20; i++) {
            Cons c = new Cons(waiter);
            new Thread(c,"消费线程"+i).start();
        }

    }
}
class Waiter {
    AtomicInteger ai = new AtomicInteger();
    BlockingQueue<String> bq = null;
    public Waiter(BlockingQueue bq) {
        this.bq = bq;
    }
    public void addCoffe() {
        String coffee = ai.incrementAndGet()+"";
        try {
            System.out.println("咖啡师"+Thread.currentThread().getName()+"生产一杯编号为"+coffee+"的coffee");
            bq.put(coffee);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void removeCoffe() {
        try {
            String c = bq.take();
            System.out.println("消费者"+Thread.currentThread().getName()+"喝了一杯编号为"+c+"的coffee");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class CoffeeMarker implements Runnable{
    private Waiter waiter;
    public CoffeeMarker(Waiter waiter) {
        System.out.println("来了一个咖啡师");
        this.waiter = waiter;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            waiter.addCoffe();
        }
    }
}
class Cons implements Runnable{
    private Waiter waiter;
    public Cons(Waiter waiter) {
        System.out.println("来了一个顾客");
        this.waiter = waiter;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            waiter.removeCoffe();
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值