KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcsimulator > listener > QueuePrinter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tcsimulator.listener;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import java.io.PrintStream JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 public class QueuePrinter implements Runnable JavaDoc {
14
15   private final LinkedQueue queue;
16   private final Collection JavaDoc outputStreams;
17
18   public QueuePrinter(LinkedQueue queue, PrintStream JavaDoc out) {
19     this.queue = queue;
20     this.outputStreams = new ArrayList JavaDoc();
21     this.outputStreams.add(out);
22   }
23
24   public void setAdditionalOutput(PrintStream JavaDoc out) {
25     this.outputStreams.add(out);
26   }
27
28   public void run() {
29     while (true) {
30       try {
31         Object JavaDoc obj = this.queue.take();
32         for (Iterator JavaDoc i = outputStreams.iterator(); i.hasNext();) {
33           ((PrintStream JavaDoc) i.next()).println(obj);
34         }
35       } catch (InterruptedException JavaDoc e) {
36         throw new RuntimeException JavaDoc(e);
37       }
38     }
39   }
40
41 }
42
Popular Tags