diff --git a/src/main/java/org/usb4java/javax/AbstractIrpQueue.java b/src/main/java/org/usb4java/javax/AbstractIrpQueue.java index 39add76..0de6cb6 100644 --- a/src/main/java/org/usb4java/javax/AbstractIrpQueue.java +++ b/src/main/java/org/usb4java/javax/AbstractIrpQueue.java @@ -65,17 +65,19 @@ public final void add(final T irp) // Start the queue processor if not already running. if (this.processor == null) { - this.processor = new Thread(new Runnable() - { - @Override - public void run() - { - process(); + synchronized(this) { + if (this.processor == null) { + this.processor = new Thread(new Runnable() { + @Override + public void run() { + process(); + } + }); + this.processor.setDaemon(true); + this.processor.setName("usb4java IRP Queue Processor"); + this.processor.start(); } - }); - this.processor.setDaemon(true); - this.processor.setName("usb4java IRP Queue Processor"); - this.processor.start(); + } } } @@ -92,7 +94,7 @@ final void process() // if present). if (irp == null) { - this.processor = null; + synchronized(this) { this.processor = null; } } else { @@ -111,7 +113,7 @@ final void process() // Get next IRP and mark the thread as closing before sending // the events for the previous IRP final T nextIrp = this.irps.poll(); - if (nextIrp == null) this.processor = null; + if (nextIrp == null) synchronized(this) { this.processor = null; } // Finish the previous IRP irp.complete(); @@ -153,7 +155,7 @@ final void process() * aborted. This method returns as soon as no more IRPs are in the queue and * no more are processed. */ - public final void abort() + public final synchronized void abort() { this.aborting = true; this.irps.clear(); @@ -180,7 +182,7 @@ public final void abort() * * @return True if queue is busy, false if not. */ - public final boolean isBusy() + public final synchronized boolean isBusy() { return !this.irps.isEmpty() || this.processor != null; }