|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.dautelle.realtime.Context
com.dautelle.realtime.ConcurrentContext
This class represents a concurrent context; it is used to accelerate execution of concurrent algorithms on multi-processors systems.
When a thread enters a concurrent context, it may execute a
concurrent logic
by calling any of the
ConcurrentContext.execute(logic, arg0, arg1, ...)
static methods.
Only after all concurrent executions are completed, is the current thread allowed to exit the scope of the concurrent context (internal synchronization).
The concurrent logics are always executed within a PoolContext
,
either by a ConcurrentThread
(if one ready) or by the current
thread within an inner pool context. Consequently, Realtime
objects made available outside of the logic scope have to be
exported
.
Concurrent contexts are easy to use, and provide automatic
load-balancing between processors with almost no overhead.
To avoid thread proliferation, the maximum concurrency is limited by
ConcurrentThread.MAX
. Concurrency can also be locally
disabled
.
ConcurrentContext are extremely efficient in reducing garbage and can often be used in place of pool contexts for this purpose. Here is an example of concurrent/recursive/clean (no garbage generated) implementation of the Karatsuba multiplication for large integers:
public LargeInteger multiply(LargeInteger that) { if (that._size <= 1) { return multiply(that.longValue()); // Direct multiplication. } else { // Karatsuba multiplication in O(nLog(3)) int bitLength = this.bitLength(); int n = (bitLength >> 1) + (bitLength & 1); FastMap results = FastMap.newInstance(3); ConcurrentContext.enter(); try { // this = a + 2^n b, that = c + 2^n d LargeInteger b = this.shiftRight(n); LargeInteger a = this.subtract(b.shiftLeft(n)); LargeInteger d = that.shiftRight(n); LargeInteger c = that.subtract(d.shiftLeft(n)); ConcurrentContext.execute(MULTIPLY, a, c, "ac", results); ConcurrentContext.execute(MULTIPLY, b, d, "bd", results); ConcurrentContext.execute(MULTIPLY, a.add(b), c.add(d), "abcd", results); } finally { ConcurrentContext.exit(); } LargeInteger ac = (LargeInteger) results.get("ac"); LargeInteger bd = (LargeInteger) results.get("bd"); LargeInteger abcd = (LargeInteger) results.get("abcd"); return ac.add(abcd.subtract(ac).subtract(bd).shiftLeft(n)).add(bd.shiftLeft(2 * n)); } } private static final Logic MULTIPLY = new Logic() { public void run(Object[] args) { LargeInteger left = (LargeInteger) args[0]; LargeInteger right = (LargeInteger) args[1]; LargeInteger product = left.multiply(right); // Recursive. FastMap results = (FastMap) args[3]; synchronized (results) { results.put(args[2], product.export()); } } };
Finally, it should be noted that any exceptions raised during concurrent
logic executions are propagated to the current thread upon
exit()
of the concurrent context.
Nested Class Summary | |
static class |
ConcurrentContext.Logic
This abstract class represents a concurrent logic. |
Method Summary | |
static void |
enter()
Enters a ConcurrentContext . |
static void |
execute(ConcurrentContext.Logic logic)
Executes the specified logic by a ConcurrentThread when possible. |
static void |
execute(ConcurrentContext.Logic logic,
java.lang.Object arg0)
Executes the specified logic with the specified argument. |
static void |
execute(ConcurrentContext.Logic logic,
java.lang.Object arg0,
java.lang.Object arg1)
Executes the specified logic with the specified two arguments. |
static void |
execute(ConcurrentContext.Logic logic,
java.lang.Object arg0,
java.lang.Object arg1,
java.lang.Object arg2)
Executes the specified logic with the specified three arguments. |
static void |
execute(ConcurrentContext.Logic logic,
java.lang.Object arg0,
java.lang.Object arg1,
java.lang.Object arg2,
java.lang.Object arg3)
Executes the specified logic with the specified four arguments. |
static void |
execute(ConcurrentContext.Logic logic,
java.lang.Object arg0,
java.lang.Object arg1,
java.lang.Object arg2,
java.lang.Object arg3,
java.lang.Object arg4)
Executes the specified logic with the specified five arguments. |
static void |
execute(ConcurrentContext.Logic logic,
java.lang.Object arg0,
java.lang.Object arg1,
java.lang.Object arg2,
java.lang.Object arg3,
java.lang.Object arg4,
java.lang.Object arg5)
Executes the specified logic with the specified six arguments. |
static void |
exit()
Exits the ConcurrentContext . |
static boolean |
isEnabled()
Indicates if concurrency is locally enabled
(default true ). |
static void |
setEnabled(boolean enabled)
Enables/disables local concurrency. |
Methods inherited from class com.dautelle.realtime.Context |
current, getInner, getOuter, getOwner, pop, push, push |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static void setEnabled(boolean enabled)
local
concurrency.
enabled
- true
if concurrency is locally enabled;
false
otherwise.public static boolean isEnabled()
locally
enabled
(default true
).
true
if concurrency is locally enabled;
false
otherwise.public static void enter()
ConcurrentContext
.
public static void execute(ConcurrentContext.Logic logic)
ConcurrentThread
when possible.
The specified logic is always executed within a PoolContext
and inherits the context of the parent thread. Any exception or error
during execution will be propagated to the current thread upon
exit()
of the concurrent context.
logic
- the logic to execute concurrently when possible.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.public static void execute(ConcurrentContext.Logic logic, java.lang.Object arg0)
logic
- the logic to execute concurrently when possible.arg0
- the logic argument.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.execute(ConcurrentContext.Logic)
public static void execute(ConcurrentContext.Logic logic, java.lang.Object arg0, java.lang.Object arg1)
logic
- the logic to execute concurrently when possible.arg0
- the first argument.arg1
- the second argument.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.execute(ConcurrentContext.Logic)
public static void execute(ConcurrentContext.Logic logic, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2)
logic
- the logic to execute concurrently when possible.arg0
- the first argument.arg1
- the second argument.arg2
- the third argument.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.execute(ConcurrentContext.Logic)
public static void execute(ConcurrentContext.Logic logic, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
logic
- the logic to execute concurrently when possible.arg0
- the first argument.arg1
- the second argument.arg2
- the third argument.arg3
- the fourth argument.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.execute(ConcurrentContext.Logic)
public static void execute(ConcurrentContext.Logic logic, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)
logic
- the logic to execute concurrently when possible.arg0
- the first argument.arg1
- the second argument.arg2
- the third argument.arg3
- the fourth argument.arg4
- the fifth argument.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.execute(ConcurrentContext.Logic)
public static void execute(ConcurrentContext.Logic logic, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5)
logic
- the logic to execute concurrently when possible.arg0
- the first argument.arg1
- the second argument.arg2
- the third argument.arg3
- the fourth argument.arg4
- the fifth argument.arg5
- the sixth argument.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.execute(ConcurrentContext.Logic)
public static void exit() throws ConcurrentException
ConcurrentContext
. This method blocks until all
concurrent executions within the current context are completed.
Errors and exceptions raised in concurrent threads are propagated here.
java.lang.ClassCastException
- if the current context is not a
ConcurrentContext
.
ConcurrentException
- propagates any error or exception raised
during the execution of a concurrent logic.
|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |