同步队列与生产者和消费者 : 生产者和消费者 « 线 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 线 » 生产者和消费者 
10. 13. 4. 同步队列与生产者和消费者
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. 生产者和消费者
10. 13. 1. Producer and comsumer with DataInputStream and DataOutputStream
10. 13. 2. Producer and consumer based on ReadableByteChannel and WritableByteChannel
10. 13. 3. 生产者,消费者和队列
10. 13. 4. 同步队列与生产者和消费者
10. 13. 5. A queue(LinkedList) is used to coordinate work between a producer and a set of worker threads.
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.