ProducerConsumerInDemo

package com.smart.thread;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
/**

  • Simple Java program to demonstrate How to use wait, notify and notifyAll()
  • method in Java by solving producer consumer problem.
  • @author Javin Paul
    */
    public class ProducerConsumerInDemo {
    public static void main(String args[]) {
    System.out.println("How to use wait and notify method in Java");
    System.out.println("Solving Producer Consumer Problem");
    Queue<Integer> buffer = new LinkedList<Integer>();
    int maxSize = 10;
    Thread producer = new Producer(buffer, maxSize, "PRODUCER");
    Thread consumer = new Consumer(buffer, maxSize, "CONSUMER");
    producer.start();
    consumer.start();
    }
    }

/**

  • Producer Thread will keep producing values for Consumer
  • to consumer. It will use wait() method when Queue is full
  • and use notify() method to send notification to Consumer
  • Thread.
  • @author WINDOWS 8

*/
class Producer extends Thread {
private final Queue<Integer> queue;
private int maxSize;
public Producer(Queue<Integer> queue, int maxSize, String name){
super(name);
this.queue = queue;
this.maxSize = maxSize;
}

@Override
public void run() {
    while (true) {
        synchronized (queue) {
            while (queue.size() == maxSize) {
                try {
                    System.out .println("Queue is full, " + "Producer thread waiting for " + "consumer to take something from queue");
                    queue.wait();
                } catch (Exception ex) {
                    ex.printStackTrace(); }
            }
            Random random = new Random();
            int i = random.nextInt();
            System.out.println("Producing value : " + i);
            queue.add(i);
            queue.notify();
        }
    }
}

}

/**

  • Consumer Thread will consumer values form shared queue.
  • It will also use wait() method to wait if queue is
  • empty. It will also use notify method to send
  • notification to producer thread after consuming values
  • from queue.
  • @author WINDOWS 8

*/
class Consumer extends Thread {
private final Queue<Integer> queue;
public Consumer(Queue<Integer> queue, int maxSize, String name){
super(name);
this.queue = queue;
}

@Override
public void run() {
    while (true) {
        synchronized (queue) {
            while (queue.isEmpty()) {
                System.out.println("Queue is empty," + "Consumer thread is waiting" + " for producer thread to put something in queue");
                try {
                    queue.wait();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            System.out.println("Consuming value : " + queue.remove());
            queue.notify();
        }
    }
}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,463评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,955评论 18 399
  • Java is a multi threaded programming language. A multi-th...
    Hackjutsu阅读 4,075评论 0 1
  • 今天去看了《28岁未成年》,最初想要去看这部电影是因为看到宣传海报上写道:“哭成傻瓜,笑回少女”。 如今越来越多的...
    王子月阅读 1,911评论 0 1
  • 在我还是个小小男孩时 我画了一棵树 它的树叶儿是水蓝色的 它的根是彩色的 小朋友都嘲笑我,他们扬着眉怪声怪气和我讲...
    马背酒阅读 2,271评论 0 0