当前位置:首页 > 开发 > 编程语言 > 多线程 > 正文

【Java多线程二】多路条件解决生产者消费者问题

发表于: 2014-10-01   作者:bit1129   来源:转载   浏览:
摘要: package com.tom; import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.locks.Condition; import java.util.concurrent.loc
package com.tom;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Plate<T> {
    private final Lock lock = new ReentrantLock();
    private final Condition PLATE_NOT_FULL = lock.newCondition();
    private final Condition PLATE_NOT_EMPTY = lock.newCondition();

    private final Queue<T> plates = new LinkedList<T>();
    private int size;

    public Plate(int size) {
        this.size = size;
    }

    public void produce(T fruit) {
        lock.lock();
        try {
            if (plates.size() >= size) {
                try {
                    System.out.println(Thread.currentThread().getName() + ", The plate is full..waiting to put into the plate");
                    PLATE_NOT_FULL.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + ", The plate is not full..put the fruit  into the plate: " + fruit);
            plates.offer(fruit);
            PLATE_NOT_EMPTY.signalAll();
        } finally {
            if (lock != null){
                lock.unlock();
            }
        }

    }

    public T consume() {
        T fruit = null;
        lock.lock();
        try {
            if (plates.isEmpty()) {
                try {
                    System.out.println(Thread.currentThread().getName() + ", The plate is empty..waiting to consume");
                    PLATE_NOT_EMPTY.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            fruit = plates.poll();
            System.out.println(Thread.currentThread().getName() + ",The plate is not empty, consume the fruit: " + fruit);
            PLATE_NOT_FULL.signalAll();
        } finally {
            if (lock != null){
                lock.unlock();
            }
        }

        return fruit;
    }

}

public class ThreadCommunication {

    private static volatile boolean shutdown = false;

    public static void main(String[] args) {

        final Plate<Integer> plate = new Plate<Integer>(8);

        Thread[] consumers = new Thread[3];

        for (int i = 0; i < consumers.length; i++) {

            consumers[i] = new Thread() {
                @Override
                public void run() {

                    while (!shutdown) {
                        plate.consume();
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            };
            consumers[i].start();
        }


        Thread[] producers = new Thread[2];

        for (int i = 0; i < producers.length; i++) {
            producers[i] = new Thread() {
                @Override
                public void run() {
                    while (!shutdown) {
                        plate.produce(ThreadLocalRandom.current().nextInt(100, 1000));
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            producers[i].start();
        }

        try {
            Thread.sleep(6000);
            shutdown = true;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

 

【Java多线程二】多路条件解决生产者消费者问题

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
在操作系统中有一类问题被称为生产者消费者问题:意为,有数个生产者生产产品,有数个消费者消费产
前言:最近在学习Java多线程,看到ImportNew网上有网友翻译的一篇文章《阻塞队列实现生产者消费者模
最近在学习多线程,自己尝试写了下生产者消费者问题, 思路: 1、使用两个信号量表示缓冲区中可以取
一、整体代码 ThreadDemo.java public class ThreadDemo { public static void main(String[] args)
之前感觉很简单,但是有一次面试让我在纸上写,居然没写对丢人啊。 生产者消费者问题(Producer-con
Java 语言实现生产者 -- 消费者模式,本文采用1个生产者和3个消费者,以体现多线程并发过程中应该注
Java 多线程编程(生产者和消费者问题以及死锁) Godown: 就是个JAVABEAN,是被执行者,即被各种线
继经典线程同步问题之后,我们来看看生产者消费者问题及读者写者问题。生产者消费者问题是一个著名
继经典线程同步问题之后,我们来看看生产者消费者问题及读者写者问题。生产者消费者问题是一个著名
继 经典线程同步问题 之后,我们来看看生产者消费者问题及读者写者问题。生产者消费者问题是一个著
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号