From 60a31c5197557cf87021ac47f5e71e0e714dafcf Mon Sep 17 00:00:00 2001 From: Kevin Harrington Date: Sun, 8 Feb 2015 18:31:21 -0500 Subject: [PATCH] This update makes the processor thread stand alone By not r-starting a thread over and over this allows for much faster communications. The primary savings is the lack of random thread locks for hundreds of ms. --- .../org/usb4java/javax/AbstractIrpQueue.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/usb4java/javax/AbstractIrpQueue.java b/src/main/java/org/usb4java/javax/AbstractIrpQueue.java index 39add76..b1240d4 100644 --- a/src/main/java/org/usb4java/javax/AbstractIrpQueue.java +++ b/src/main/java/org/usb4java/javax/AbstractIrpQueue.java @@ -31,7 +31,7 @@ abstract class AbstractIrpQueue private final Queue irps = new ConcurrentLinkedQueue(); /** The queue processor thread. */ - private volatile Thread processor; + private volatile Thread processor=null; /** If queue is currently aborting. */ private volatile boolean aborting; @@ -70,7 +70,17 @@ public final void add(final T irp) @Override public void run() { - process(); + while(processor != null){ + if(!irps.isEmpty()) + process(); + else + try { + Thread.sleep(0,1); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } } }); this.processor.setDaemon(true); @@ -82,7 +92,7 @@ public void run() /** * Processes the queue. Methods returns when the queue is empty. */ - final void process() + void process() { // Get the next IRP T irp = this.irps.poll(); @@ -90,11 +100,7 @@ final void process() // If there are no IRPs to process then mark the thread as closing // right away. Otherwise process the IRP (and more IRPs from the queue // if present). - if (irp == null) - { - this.processor = null; - } - else + if (irp != null) { while (irp != null) { @@ -111,7 +117,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) this.processor = null; // Finish the previous IRP irp.complete(); @@ -157,6 +163,7 @@ public final void abort() { this.aborting = true; this.irps.clear(); + processor=null; while (isBusy()) { try @@ -182,7 +189,7 @@ public final void abort() */ public final boolean isBusy() { - return !this.irps.isEmpty() || this.processor != null; + return !this.irps.isEmpty() ; } /**