From ab5c148f37551086f56070f5f6ed676961246df0 Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Sun, 24 Feb 2013 16:30:36 +0100 Subject: [PATCH] Switch from synchronized list to thread-safe queue to speed up asynchronous operations --- .../usb4java/support/AbstractIrpQueue.java | 100 ++++++++---------- 1 file changed, 47 insertions(+), 53 deletions(-) diff --git a/src/main/java/de/ailis/usb4java/support/AbstractIrpQueue.java b/src/main/java/de/ailis/usb4java/support/AbstractIrpQueue.java index 30f387d..35cd69a 100644 --- a/src/main/java/de/ailis/usb4java/support/AbstractIrpQueue.java +++ b/src/main/java/de/ailis/usb4java/support/AbstractIrpQueue.java @@ -5,8 +5,8 @@ package de.ailis.usb4java.support; -import java.util.Deque; -import java.util.LinkedList; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; import javax.usb.UsbException; import javax.usb.UsbHostManager; @@ -25,7 +25,7 @@ import de.ailis.usb4java.topology.Usb4JavaDevice; public abstract class AbstractIrpQueue { /** The queued packets. */ - private final Deque irps = new LinkedList(); + private final Queue irps = new ConcurrentLinkedQueue(); /** The queue processor thread. */ private Thread processor; @@ -52,24 +52,21 @@ public abstract class AbstractIrpQueue */ public final void add(final T irp) { - synchronized (this.irps) - { - this.irps.add(irp); + this.irps.add(irp); - // Start the queue processor if not already running. - if (this.processor == null) + // Start the queue processor if not already running. + if (this.processor == null) + { + this.processor = new Thread(new Runnable() { - this.processor = new Thread(new Runnable() + @Override + public void run() { - @Override - public void run() - { - process(); - } - }); - this.processor.setDaemon(true); - this.processor.start(); - } + process(); + } + }); + this.processor.setDaemon(true); + this.processor.start(); } } @@ -80,39 +77,39 @@ public abstract class AbstractIrpQueue { while (true) { - synchronized (this.irps) - { - // Get the next IRP - final T irp = this.irps.poll(); + // Get the next IRP + final T irp = this.irps.poll(); - // Process the IRP + // Process the IRP + try + { + UsbLock.acquire(); try { - UsbLock.acquire(); - try - { - processIrp(irp); - } - finally - { - UsbLock.release(); - } + processIrp(irp); } - catch (final UsbException e) + finally { - irp.setUsbException(e); + UsbLock.release(); } - irp.complete(); - finishIrp(irp); + } + catch (final UsbException e) + { + irp.setUsbException(e); + } + irp.complete(); + finishIrp(irp); - // When no more IRPs are present in the queue then terminate - // the thread. - if (this.irps.isEmpty()) + // When no more IRPs are present in the queue then terminate + // the thread. + if (this.irps.isEmpty()) + { + this.processor = null; + synchronized (this.irps) { - this.processor = null; this.irps.notifyAll(); - break; } + break; } } } @@ -143,19 +140,19 @@ public abstract class AbstractIrpQueue */ public final void abort() { - synchronized (this.irps) + this.irps.clear(); + while (isBusy()) { - this.irps.clear(); - while (isBusy()) + try { - try + synchronized (this.irps) { this.irps.wait(); } - catch (final InterruptedException e) - { - Thread.currentThread().interrupt(); - } + } + catch (final InterruptedException e) + { + Thread.currentThread().interrupt(); } } } @@ -168,10 +165,7 @@ public abstract class AbstractIrpQueue */ public final boolean isBusy() { - synchronized (this.irps) - { - return !this.irps.isEmpty() || this.processor != null; - } + return !this.irps.isEmpty() || this.processor != null; } /**