Skip to content
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

This update makes the processor thread stand alone #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/main/java/org/usb4java/javax/AbstractIrpQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractIrpQueue<T extends UsbIrp>
private final Queue<T> irps = new ConcurrentLinkedQueue<T>();

/** The queue processor thread. */
private volatile Thread processor;
private volatile Thread processor=null;

/** If queue is currently aborting. */
private volatile boolean aborting;
Expand Down Expand Up @@ -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);
Expand All @@ -82,19 +92,15 @@ 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();

// 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)
{
Expand All @@ -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();
Expand Down Expand Up @@ -157,6 +163,7 @@ public final void abort()
{
this.aborting = true;
this.irps.clear();
processor=null;
while (isBusy())
{
try
Expand All @@ -182,7 +189,7 @@ public final void abort()
*/
public final boolean isBusy()
{
return !this.irps.isEmpty() || this.processor != null;
return !this.irps.isEmpty() ;
}

/**
Expand Down