FileDocCategorySizeDatePackage
Consumer.javaAPI DocExample728Tue May 04 09:43:44 BST 2004com.oreilly.tiger.ch10

Consumer.java

package com.oreilly.tiger.ch10;

import java.io.PrintStream;
import java.util.concurrent.BlockingQueue;

public class Consumer extends Thread {
  
  private BlockingQueue q;
  private PrintStream out;

  public Consumer(String name, BlockingQueue q, 
                  PrintStream out) {
    setName(name);
    this.q = q;
    this.out = out;
  }

  public void run() {
    try {
      while (true) {
        process(q.take());
      }
    } catch (InterruptedException e) {
      out.printf("%s interrupted: %s", getName(), e.getMessage());
    }
  }

  private void process(Object obj) {
    out.printf("%s processing object:%n         '%s'%n", 
                getName(), obj.toString());
  }
}