KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > awt > RunQueue


1 // $Id: RunQueue.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // A queue for runnable tasks
4

5 /*
6  * Copyright 1998 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk.awt;
24
25 import java.util.Vector JavaDoc;
26
27 /**
28  * A queue for runnable tasks.
29  * The run method sequentially invokes tasks in the queue.
30  * The run queue is meant to be run in its own thread.
31  * @version January 1998
32  * @author John D. Ramsdell
33  */

34 public class RunQueue
35 implements Runnable JavaDoc
36 {
37   // A queue of runnables
38
private final Vector JavaDoc queue = new Vector JavaDoc();
39
40   // Indicates that tasks extracted from the queue should not be started
41
private volatile boolean interrupt = false;
42
43   // Indicates that the run method should exit
44
private volatile boolean poisoned = false;
45
46   /**
47    * Stop the run method.
48    * To stop the run method, poison it,
49    * and then interrupt the thread running it.
50    */

51   public void setPoisoned(boolean poisoned) {
52     this.poisoned = poisoned;
53   }
54
55   /**
56    * Add a task to the run queue.
57    * @param task a runnable task
58    */

59   public synchronized void add(Runnable JavaDoc task) {
60     queue.addElement(task);
61     notify();
62   }
63
64   /**
65    * Remove all tasks from the run queue.
66    */

67   public synchronized void removeAll() {
68     interrupt = true; // Stop all tasks that
69
queue.removeAllElements(); // have not been started
70
}
71
72   /**
73    * Runs tasks in a task queue.
74    */

75   public void run() {
76     Vector JavaDoc q;
77     while (!poisoned) {
78       try {
79     synchronized (this) {
80       while (queue.isEmpty())
81         wait();
82       q = (Vector JavaDoc)queue.clone(); // Extract tasks from the queue
83
queue.removeAllElements();
84       interrupt = false;
85     }
86       }
87       catch (InterruptedException JavaDoc ie) {
88     continue; // q is null so just continue loop
89
}
90       // Abort loop when the interrupt is enabled
91
for (int i = 0; i < q.size() && !interrupt; i++) {
92     Runnable JavaDoc task = (Runnable JavaDoc)q.elementAt(i);
93     try {
94       task.run();
95     }
96     catch (Throwable JavaDoc t) {
97       System.err.println("Internal error: " + t.getMessage());
98       t.printStackTrace();
99     }
100       }
101       q = null; // Recycle queue.
102
}
103   }
104 }
105
Popular Tags