-
Notifications
You must be signed in to change notification settings - Fork 106
fix: incorporate feedback from #138 #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2ef07e0
9dcc9b3
f791604
19c6c7c
f69db2a
fc9d35a
035d63f
8ae2199
500c89a
483ee20
2e424b1
4ef2fcb
5736b46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.LinkedBlockingDeque; | ||
| import java.util.concurrent.Semaphore; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
|
|
@@ -13,13 +15,17 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public abstract class EventQueue { | ||
| public abstract class EventQueue implements AutoCloseable { | ||
|
|
||
| private static final Logger log = new TempLoggerWrapper(LoggerFactory.getLogger(EventQueue.class)); | ||
|
|
||
| // TODO decide on a capacity | ||
| private static final int queueSize = 1000; | ||
|
|
||
| private final EventQueue parent; | ||
| // TODO decide on a capacity (or more appropriate queue data structures) | ||
| private final BlockingQueue<Event> queue = new ArrayBlockingQueue<Event>(1000); | ||
|
|
||
| private final BlockingQueue<Event> queue = new LinkedBlockingDeque<>(); | ||
| private final Semaphore semaphore = new Semaphore(queueSize, true); | ||
| private volatile boolean closed = false; | ||
|
|
||
|
|
||
|
|
@@ -47,6 +53,11 @@ public void enqueueEvent(Event event) { | |
| return; | ||
| } | ||
| // Call toString() since for errors we don't really want the full stacktrace | ||
| try { | ||
| semaphore.acquire(); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException("Unable to acquire the semaphore to enqueue the event", e); | ||
| } | ||
| queue.add(event); | ||
| log.debug("Enqueued event {} {}", event instanceof Throwable ? event.toString() : event, this); | ||
| } | ||
|
|
@@ -65,6 +76,7 @@ public Event dequeueEvent(int waitMilliSeconds) throws EventQueueClosedException | |
| // Call toString() since for errors we don't really want the full stacktrace | ||
| log.debug("Dequeued event (no wait) {} {}", this, event instanceof Throwable ? event.toString() : event); | ||
| } | ||
| semaphore.release(); | ||
|
||
| return event; | ||
| } | ||
| try { | ||
|
|
@@ -73,6 +85,7 @@ public Event dequeueEvent(int waitMilliSeconds) throws EventQueueClosedException | |
| // Call toString() since for errors we don't really want the full stacktrace | ||
| log.debug("Dequeued event (waiting) {} {}", this, event instanceof Throwable ? event.toString() : event); | ||
| } | ||
| semaphore.release(); | ||
|
||
| return event; | ||
| } catch (InterruptedException e) { | ||
| log.debug("Interrupted dequeue (waiting) {}", this); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When catching an
InterruptedException, it is a best practice to restore the interrupted status of the thread by callingThread.currentThread().interrupt()1. This allows code higher up the call stack to be aware that an interruption occurred and handle it appropriately. Simply wrapping it in aRuntimeExceptionloses this important information.Style Guide References
Footnotes
Restore the interrupted status (link) ↩