aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/e1/pdj/PriorityQueue.java
blob: 59a69a4b2143983231e82a4c0f7422b302dfc20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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();
		}
	}
	
}