/interpreter/tags/at2dist041108/src/edu/vub/at/actors/eventloops/EventQueue.java
Java | 120 lines | 46 code | 12 blank | 62 comment | 0 complexity | c396bbe3a00d5af2559e785d370e0cf2 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * EventQueue.java created on 21-dec-2006 at 11:30:59 4 * (c) Programming Technology Lab, 2006 - 2007 5 * Authors: Tom Van Cutsem & Stijn Mostinckx 6 * 7 * Permission is hereby granted, free of charge, to any person 8 * obtaining a copy of this software and associated documentation 9 * files (the "Software"), to deal in the Software without 10 * restriction, including without limitation the rights to use, 11 * copy, modify, merge, publish, distribute, sublicense, and/or 12 * sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following 14 * conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 * OTHER DEALINGS IN THE SOFTWARE. 27 */ 28package edu.vub.at.actors.eventloops; 29 30import java.util.Vector; 31 32/** 33 * A simple synchronized blocking FIFO queue implementation. 34 * The buffer will grow dynamically to accomodate more events when necessary. 35 * 36 * @author tvcutsem 37 */ 38public final class EventQueue { 39 40 private static final int _DEFAULT_QUEUE_SIZE_ = 10; 41 42 private final Vector elements_; 43 44 /** Constructor which creates an empty EventQueue */ 45 public EventQueue() { 46 elements_ = new Vector(_DEFAULT_QUEUE_SIZE_); 47 } 48 49 /** 50 * Enqueue an event in the buffer. This method wakes up any 51 * waiting consumer threads. 52 */ 53 public void enqueue(Event event) { 54 synchronized(this) { 55 elements_.add(event); 56 notifyAll(); 57 } 58 } 59 60 /** 61 * Enqueue an event as the first to be executed in the buffer. 62 * This method wakes up any waiting consumer threads. 63 */ 64 public void enqueueFirst(Event event) { 65 synchronized(this) { 66 elements_.add(0, event); 67 notifyAll(); 68 } 69 } 70 71 /** 72 * Dequeue an event from the buffer. This method will block when the 73 * buffer is empty! 74 * 75 * @return the dequeued event. 76 * @throws InterruptedException if the thread is interrupted while waiting on an empty buffer. 77 */ 78 public Event dequeue() throws InterruptedException { 79 synchronized(this) { 80 while(elements_.isEmpty()) { 81 wait(); 82 } 83 return (Event) elements_.remove(0); 84 } 85 } 86 87 /** 88 * Clears the content of the buffer and returns the old contents. 89 * @return the buffer's content at the time it was flushed 90 */ 91 public Vector flush() { 92 synchronized(this) { 93 Vector copy = (Vector) elements_.clone(); 94 elements_.clear(); 95 return copy; 96 } 97 } 98 99 /** 100 * Allows for restoring the buffer's old contents by putting 101 * the elements in contents in front of the buffer. 102 */ 103 public void merge(Vector contents) { 104 synchronized(this) { 105 elements_.addAll(0, contents); 106 } 107 } 108 109 /** 110 * Tests whether the event queue is empty. This test is provided to 111 * allow clients of the queue to perform synchronization on an external 112 * resource if necessary. 113 */ 114 public boolean isEmpty() { 115 synchronized (this) { 116 return elements_.isEmpty(); 117 } 118 } 119 120}