JADE v6.1

com.dautelle.realtime
Class ConcurrentContext

java.lang.Object
  extended bycom.dautelle.realtime.Context
      extended bycom.dautelle.realtime.ConcurrentContext

public final class ConcurrentContext
extends Context

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.

Version:
6.0, May 24, 2003
Author:
Jean-Marie Dautelle

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

setEnabled

public static void setEnabled(boolean enabled)
Enables/disables local concurrency.

Parameters:
enabled - true if concurrency is locally enabled; false otherwise.

isEnabled

public static boolean isEnabled()
Indicates if concurrency is locally enabled (default true).

Returns:
true if concurrency is locally enabled; false otherwise.

enter

public static void enter()
Enters a ConcurrentContext.


execute

public static void execute(ConcurrentContext.Logic logic)
Executes the specified logic by a 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.

Parameters:
logic - the logic to execute concurrently when possible.
Throws:
java.lang.ClassCastException - if the current context is not a ConcurrentContext.

execute

public static void execute(ConcurrentContext.Logic logic,
                           java.lang.Object arg0)
Executes the specified logic with the specified argument.

Parameters:
logic - the logic to execute concurrently when possible.
arg0 - the logic argument.
Throws:
java.lang.ClassCastException - if the current context is not a ConcurrentContext.
See Also:
execute(ConcurrentContext.Logic)

execute

public static void execute(ConcurrentContext.Logic logic,
                           java.lang.Object arg0,
                           java.lang.Object arg1)
Executes the specified logic with the specified two arguments.

Parameters:
logic - the logic to execute concurrently when possible.
arg0 - the first argument.
arg1 - the second argument.
Throws:
java.lang.ClassCastException - if the current context is not a ConcurrentContext.
See Also:
execute(ConcurrentContext.Logic)

execute

public 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.

Parameters:
logic - the logic to execute concurrently when possible.
arg0 - the first argument.
arg1 - the second argument.
arg2 - the third argument.
Throws:
java.lang.ClassCastException - if the current context is not a ConcurrentContext.
See Also:
execute(ConcurrentContext.Logic)

execute

public 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.

Parameters:
logic - the logic to execute concurrently when possible.
arg0 - the first argument.
arg1 - the second argument.
arg2 - the third argument.
arg3 - the fourth argument.
Throws:
java.lang.ClassCastException - if the current context is not a ConcurrentContext.
See Also:
execute(ConcurrentContext.Logic)

execute

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)
Executes the specified logic with the specified five arguments.

Parameters:
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.
Throws:
java.lang.ClassCastException - if the current context is not a ConcurrentContext.
See Also:
execute(ConcurrentContext.Logic)

execute

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)
Executes the specified logic with the specified six arguments.

Parameters:
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.
Throws:
java.lang.ClassCastException - if the current context is not a ConcurrentContext.
See Also:
execute(ConcurrentContext.Logic)

exit

public static void exit()
                 throws ConcurrentException
Exits the ConcurrentContext. This method blocks until all concurrent executions within the current context are completed. Errors and exceptions raised in concurrent threads are propagated here.

Throws:
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

Copyright © 2004 Jean-Marie Dautelle.