aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/e1/pdj/PriorityQueue.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/java/com/e1/pdj/PriorityQueue.java')
-rw-r--r--src/java/com/e1/pdj/PriorityQueue.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/java/com/e1/pdj/PriorityQueue.java b/src/java/com/e1/pdj/PriorityQueue.java
new file mode 100644
index 0000000..59a69a4
--- /dev/null
+++ b/src/java/com/e1/pdj/PriorityQueue.java
@@ -0,0 +1,68 @@
+package com.e1.pdj;
+
+import java.util.*;
+import com.cycling74.max.Executable;
+
+public class PriorityQueue implements Runnable {
+ List list = new ArrayList();
+ private Thread thread;
+ boolean tostop = false;
+
+ public PriorityQueue(int priority) {
+ thread = new Thread(this);
+
+ switch( priority ) {
+ case Thread.MIN_PRIORITY :
+ thread.setName("PriorityQueue:low");
+ break;
+ case Thread.NORM_PRIORITY :
+ thread.setName("PriorityQueue:norm");
+ break;
+ case Thread.MAX_PRIORITY :
+ thread.setName("PriorityQueue:max");
+ break;
+ }
+ thread.setPriority(priority);
+ thread.setDaemon(true);
+ thread.start();
+ }
+
+ public void shutdown() {
+ synchronized(this) {
+ tostop = true;
+ notify();
+ }
+ }
+
+ public void run() {
+ Executable exec;
+
+ while(true) {
+ try {
+ synchronized(this) {
+ if ( list.size() == 0 )
+ wait();
+
+ if ( tostop )
+ break;
+ exec = (Executable) list.remove(0);
+ }
+ try {
+ exec.execute();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ } catch (InterruptedException e) {
+ break;
+ }
+ }
+ }
+
+ public void defer(Executable e) {
+ synchronized(this) {
+ list.add(e);
+ notify();
+ }
+ }
+
+}