Hello,
I tried to implement some parallel algorithm, which basically starts
10 Runnables with Dispatch.getGlobalQueue().execute(Runnable), to
process 10 chunks of data and then waits for them to finish.
The self-describing output is as follows (thread name:
hawtdispatch-DEFAULT-1: chunk 0 done in 645 ms
hawtdispatch-DEFAULT-1: chunk 1 done in 644 ms
hawtdispatch-DEFAULT-1: chunk 2 done in 642 ms
hawtdispatch-DEFAULT-1: chunk 3 done in 654 ms
hawtdispatch-DEFAULT-1: chunk 4 done in 646 ms
hawtdispatch-DEFAULT-1: chunk 5 done in 641 ms
hawtdispatch-DEFAULT-1: chunk 6 done in 668 ms
hawtdispatch-DEFAULT-1: chunk 7 done in 645 ms
hawtdispatch-DEFAULT-1: chunk 8 done in 642 ms
hawtdispatch-DEFAULT-1: chunk 9 done in 642 ms
Result: 3.1415926545898296
Time: 6474 ms
Problem is that all Runnables are processed in the same thread,
sequentially.
I think that problem is in
org.fusesource.hawtdispatch.internal.NioManager.isSelecting(), which
can return false for a small time after wakeup() was called (see also
method NioManager.select(long)).
This causes SimplePool.execute(Runnable) to wakeup just the first
thread few times, when called repeatedly very fast.
I'm sure that in real production when using hawtdispatch library for
servers connection processing. this is not real issue since the
requests are with high probabliity better distributed in time. But for
usages as I used it, it is a problem.
I propose a patch, which is in fact quite easy. The patch was created
against GIT clone from 02/13/2012, 13:00
I apologize, but I cannot see "Submit new" in the issue tracker
http://www.assembla.com/spaces/hawtdispatch/support/tickets so I add
the patch here, because it is really short.
output of git diff:
diff --git a/hawtdispatch/src/main/java/org/fusesource/hawtdispatch/
internal/NioManager.java b/hawtdispatch/src/main/java/org/fusesource/
hawtdispatch/internal/NioManager.java
index 4f971f3..b6679fe 100644
--- a/hawtdispatch/src/main/java/org/fusesource/hawtdispatch/internal/
NioManager.java
+++ b/hawtdispatch/src/main/java/org/fusesource/hawtdispatch/internal/
NioManager.java
@@ -163,7 +163,7 @@ public class NioManager {
}
public boolean isSelecting() {
- return selecting;
+ return selecting && wakeupCounter == selectCounter;
}
/**
@@ -196,8 +196,8 @@ public class NioManager {
selectStrategy.select(timeout);
}
} finally {
- selectCounter = wakeupCounter;
selecting=false;
+ selectCounter = wakeupCounter;
}
}
} catch (CancelledKeyException e) {