From a764e59e1d3a8e330f0d484fdb26b35ca3f0b2e4 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Sat, 22 Mar 2008 02:15:12 +0000 Subject: bringing pdj-0.8.3 into the main branch svn path=/trunk/externals/loaders/pdj/; revision=9621 --- src/java/com/e1/pdj/PriorityQueue.java | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/java/com/e1/pdj/PriorityQueue.java (limited to 'src/java/com/e1/pdj/PriorityQueue.java') 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(); + } + } + +} -- cgit v1.2.1