/interpreter/tags/at2dist170907/src/edu/vub/at/actors/eventloops/EventQueue.java

http://ambienttalk.googlecode.com/ · 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. */
  28. package edu.vub.at.actors.eventloops;
  29. import java.util.Vector;
  30. /**
  31. * A simple synchronized blocking FIFO queue implementation.
  32. * The buffer will grow dynamically to accomodate more events when necessary.
  33. *
  34. * @author tvcutsem
  35. */
  36. public final class EventQueue {
  37. private static final int _DEFAULT_QUEUE_SIZE_ = 10;
  38. private final Vector elements_;
  39. /** Constructor which creates an empty EventQueue */
  40. public EventQueue() {
  41. elements_ = new Vector(_DEFAULT_QUEUE_SIZE_);
  42. }
  43. /**
  44. * Enqueue an event in the buffer. This method wakes up any
  45. * waiting consumer threads.
  46. */
  47. public void enqueue(Event event) {
  48. synchronized(this) {
  49. elements_.add(event);
  50. notifyAll();
  51. }
  52. }
  53. /**
  54. * Enqueue an event as the first to be executed in the buffer.
  55. * This method wakes up any waiting consumer threads.
  56. */
  57. public void enqueueFirst(Event event) {
  58. synchronized(this) {
  59. elements_.add(0, event);
  60. notifyAll();
  61. }
  62. }
  63. /**
  64. * Dequeue an event from the buffer. This method will block when the
  65. * buffer is empty!
  66. *
  67. * @return the dequeued event.
  68. * @throws InterruptedException if the thread is interrupted while waiting on an empty buffer.
  69. */
  70. public Event dequeue() throws InterruptedException {
  71. synchronized(this) {
  72. while(elements_.isEmpty()) {
  73. wait();
  74. }
  75. return (Event) elements_.remove(0);
  76. }
  77. }
  78. /**
  79. * Clears the content of the buffer and returns the old contents.
  80. * @return the buffer's content at the time it was flushed
  81. */
  82. public Vector flush() {
  83. synchronized(this) {
  84. Vector copy = (Vector) elements_.clone();
  85. elements_.clear();
  86. return copy;
  87. }
  88. }
  89. /**
  90. * Allows for restoring the buffer's old contents by putting
  91. * the elements in contents in front of the buffer.
  92. */
  93. public void merge(Vector contents) {
  94. synchronized(this) {
  95. elements_.addAll(0, contents);
  96. }
  97. }
  98. /**
  99. * Tests whether the event queue is empty. This test is provided to
  100. * allow clients of the queue to perform synchronization on an external
  101. * resource if necessary.
  102. */
  103. public boolean isEmpty() {
  104. synchronized (this) {
  105. return elements_.isEmpty();
  106. }
  107. }
  108. }