zoukankan      html  css  js  c++  java
  • java 生产者消费者简单实现demo

    第一种方式 使用BlockingQueue 阻塞队列

    public class Threads {


    public static void main(String[] args) {
    final ArrayBlockingQueue<Integer> queue=new ArrayBlockingQueue<>(10);
    produce produce=new produce(queue);
    consumer consumer=new consumer(queue);
    consumer.start();
    produce.start();

    }

    static class produce extends Thread{
    final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;

    public produce(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
    this.integerArrayBlockingQueue = integerArrayBlockingQueue;
    }

    @Override
    public void run() {
    while (true){
    Integer random=new Random().nextInt(10);
    integerArrayBlockingQueue.add(random);
    System.out.println("shen chan shu ju"+random);
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    static class consumer extends Thread{
    final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;

    public consumer(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
    this.integerArrayBlockingQueue = integerArrayBlockingQueue;
    }

    @Override

    public void run() {
    while (true){
    try {
    Integer element=integerArrayBlockingQueue.take();
    System.out.println("xiao fei shu ju "+element);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    }
    第二种方法利用 wait()和 notifyAll()
    public class Threads {
    private static String lock = "lock";

    public static void main(String[] args) {
    final List<Integer> list = new ArrayList<>(10);
    final Integer max = 10;
    produce produce1 = new produce(list, max);
    produce produce2 = new produce(list, max);
    consumer consumer = new consumer(list, max);
    consumer.start();
    produce1.start();
    produce2.start();
    }

    static class produce extends Thread {
    final List<Integer> list;
    final Integer max;

    public produce(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override
    public void run() {
    while (true) {
    synchronized (lock) {
    while (list.size() > max) {
    try {
    lock.wait();
    System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = new Random().nextInt(10);
    list.add(random);
    lock.notifyAll();
    System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
    }
    }
    }
    }

    static class consumer extends Thread {
    final List<Integer> list;
    final Integer max;

    public consumer(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override

    public void run() {
    while (true) {
    synchronized (lock) {
    while (list.isEmpty()) {
    try {
    lock.wait();
    System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = list.remove(0);
    lock.notifyAll();
    System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
    }

    }
    }
    }

    }
    第三种方法 ReentrantLock await() 和 signal() 实现
    public class Threads {

    private Lock lock=new ReentrantLock();
    final Condition notfull=lock.newCondition();
    final Condition notempty=lock.newCondition();
    public static void main(String[] args) {
    final List<Integer> list = new ArrayList<>(10);
    final Integer max = 10;
    Threads threads = new Threads();
    produce produce1 = threads.new produce(list, max);
    produce produce2 = threads.new produce(list, max);
    consumer consumer = threads.new consumer(list, max);
    consumer.start();
    produce1.start();
    produce2.start();
    }

    class produce extends Thread {
    final List<Integer> list;
    final Integer max;

    public produce(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override
    public void run() {
    while (true) {
    lock.lock();
    while (list.size() > max) {
    try {
    notfull.await();
    System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = new Random().nextInt(10);
    list.add(random);
    notempty.signal();
    System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
    }
    }
    }

    class consumer extends Thread {
    final List<Integer> list;
    final Integer max;

    public consumer(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override

    public void run() {

    while (true) {
    lock.lock();
    while (list.isEmpty()) {
    try {
    notempty.await();
    System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = list.remove(0);
    notfull.signal();
    System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
    }

    }
    }

    }


  • 相关阅读:
    野指针、NULL指针和void*
    Windows获取其他进程中Edit控件的内容
    error:LNK2005 已经在*.obj中定义
    2018-2019-1 20165226 《信息安全系统设计基础》第6周学习总结
    # 2018-2019-1 20165226 《信息安全系统设计基础》第5周学习总结
    2018-2019-1 20165310 20165315 20165226 实验一 开发环境的熟悉
    2018-2019-1 20165226 《信息安全系统设计基础》第4周学习总结
    2018-2019-1 20165226 《信息安全系统设计基础》第3周学习总结
    缓冲区溢出漏洞实验
    2018-2019-1 20165226 《信息安全系统设计基础》第2周学习总结
  • 原文地址:https://www.cnblogs.com/prettrywork/p/10233428.html
Copyright © 2011-2022 走看看