Synchronized Queue with Producer and Consumer : Producer and consumer « Thread « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Thread » Producer and consumer 
10.13.4.Synchronized Queue with Producer and ConsumerPrevious/Next
public class ThreadTester {
  public static void main(String[] args) {
    SynchronizedQueue<String> queue = new SynchronizedQueue<String>(10);
    final int GREETING_COUNT = 100;
    Runnable run1 = new Producer("Hello, World!", queue, GREETING_COUNT);
    Runnable run2 = new Producer("Goodbye, World!", queue, GREETING_COUNT);
    Runnable run3 = new Consumer(queue, * GREETING_COUNT);

    Thread thread1 = new Thread(run1);
    Thread thread2 = new Thread(run2);
    Thread thread3 = new Thread(run3);

    thread1.start();
    thread2.start();
    thread3.start();
  }
}

class Producer implements Runnable {
  private String greeting;
  private SynchronizedQueue<String> queue;

  private int greetingCount;

  public Producer(String aGreeting, SynchronizedQueue<String> aQueue, int count) {
    greeting = aGreeting;
    queue = aQueue;
    greetingCount = count;
  }

  public void run() {
    try {
      int i = 1;
      while (i <= greetingCount) {
        queue.add(i + ": " + greeting);
        i++;
        Thread.sleep(2000);
      }
    catch (InterruptedException exception) {
    }
  }

}

class Consumer implements Runnable {

  private SynchronizedQueue<String> queue;

  private int greetingCount;

  public Consumer(SynchronizedQueue<String> aQueue, int count) {
    queue = aQueue;
    greetingCount = count;
  }

  public void run() {
    try {
      int i = 1;
      while (i <= greetingCount) {
        String greeting = queue.remove();
        System.out.println(greeting);
        i++;
        Thread.sleep(3000);
      }
    catch (InterruptedException exception) {
    }
  }

}

class SynchronizedQueue<V> {

  private Object[] elements;

  private int head;

  private int tail;

  private int size;

  public SynchronizedQueue(int capacity) {
    elements = new Object[capacity];
    head = 0;
    tail = 0;
    size = 0;
  }

  public synchronized V remove() throws InterruptedException {
    while (size == 0)
      wait();
    V r = (Velements[head];
    head++;
    size--;
    if (head == elements.length)
      head = 0;
    notifyAll();
    return r;
  }

  public synchronized void add(V newValuethrows InterruptedException {
    while (size == elements.length)
      wait();
    elements[tail= newValue;
    tail++;
    size++;
    if (tail == elements.length)
      tail = 0;
    notifyAll();
  }

}
10.13.Producer and consumer
10.13.1.Producer and comsumer with DataInputStream and DataOutputStream
10.13.2.Producer and consumer based on ReadableByteChannel and WritableByteChannel
10.13.3.Producer, consumer and Queue
10.13.4.Synchronized Queue with Producer and Consumer
10.13.5.A queue(LinkedList) is used to coordinate work between a producer and a set of worker threads.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.