Javolution 6.0.0 java
javolution.context.ConcurrentContext Class Referenceabstract
Inheritance diagram for javolution.context.ConcurrentContext:
[legend]
Collaboration diagram for javolution.context.ConcurrentContext:
[legend]

Public Member Functions

abstract void execute (Runnable logic)
 
abstract void setConcurrency (int concurrency)
 
abstract int getConcurrency ()
 
void exit ()
 

Static Public Member Functions

static ConcurrentContext enter ()
 
static void execute (Runnable... logics)
 
static AbstractContext current ()
 
static< T extends AbstractContext > T enter (Class< T > custom)
 
static void inherit (AbstractContext ctx)
 

Static Public Attributes

static final Configurable< Integer > CONCURRENCY
 

Protected Member Functions

 ConcurrentContext ()
 
AbstractContext enterInner ()
 
AbstractContext getOuter ()
 
abstract AbstractContext inner ()
 

Static Protected Member Functions

static< T extends AbstractContext > T current (Class< T > type)
 

Private Attributes

AbstractContext outer
 

Static Private Attributes

static final ThreadLocal< AbstractContextCURRENT = new ThreadLocal<AbstractContext>()
 

Detailed Description

A context able to take advantage of concurrent algorithms on multi-processors systems.

When a thread enters a concurrent context, it may performs concurrent executions by calling the execute(Runnable) static method. The logic is then executed by a concurrent thread or by the current thread itself if there is no concurrent thread immediately available (the number of concurrent threads is limited, see CONCURRENCY). [code] ConcurrentContext ctx = ConcurrentContext.enter(); try { ctx.execute(new Runnable() {...}); ctx.execute(new Runnable() {...});
} finally { ctx.exit(); // Waits for all concurrent executions to complete. // Re-exports any exception raised during concurrent executions. }[/code]

or equivalent shorter notation: [code] ConcurrentContext.execute(new Runnable() {...}, new Runnable() {...});[/code]

Only after all concurrent executions are completed, is the current thread allowed to exit the scope of the concurrent context (internal synchronization).

Concurrent logics always execute within the same context as the calling thread.

Concurrent contexts ensure the same behavior whether or not the execution is performed by the current thread or a concurrent thread. Any error or runtime exception raised during the concurrent logic executions is propagated to the current thread.

Concurrent contexts are easy to use, and provide automatic load-balancing between processors with almost no overhead. Here is a concurrent/recursive quick/merge sort using anonymous inner classes. [code] static void concurrentSort(final FastTable<? extends Comparable> table) { final int size = table.size(); if (size < 100) { table.sort(); // Direct quick sort. } else { // Splits table in two and sort both part concurrently. final FastTable<? extends Comparable> t1 = new FastTable(); final FastTable<? extends Comparable> t2 = new FastTable(); ConcurrentContext ctx = ConcurrentContext.enter(); try { ctx.execute(new Runnable() { public void run() { t1.addAll(table.subList(0, size / 2)); concurrentSort(t1); // Recursive. } }); ctx.execute(new Runnable() { public void run() { t2.addAll(table.subList(size / 2, size)); concurrentSort(t2); // Recursive. } }); } finally { ctx.exit(); // Joins. } // Merges results. for (int i=0, i1=0, i2=0; i < size; i++) { if (i1 >= t1.size()) { table.set(i, t2.get(i2++)); } else if (i2 >= t2.size()) { table.set(i, t1.get(i1++)); } else { Comparable o1 = t1.get(i1); Comparable o2 = t2.get(i2); if (o1.compareTo(o2) < 0) { table.set(i, o1); i1++; } else { table.set(i, o2); i2++; } } } } }[/code]

Here is another example using execute(Runnable ...) static method (Karatsuba recursive multiplication for large integers). [code] public LargeInteger multiply(LargeInteger that) { if (that._size <= 1) { return multiply(that.longValue()); // Direct multiplication. } else { // Karatsuba multiplication in O(n^log2(3)) int bitLength = this.bitLength(); int n = (bitLength >> 1) + (bitLength & 1);

// this = a + 2^n b, that = c + 2^n d LargeInteger b = this.shiftRight(n); LargeInteger a = this.minus(b.shiftLeft(n)); LargeInteger d = that.shiftRight(n); LargeInteger c = that.minus(d.shiftLeft(n)); Multiply ac = new Multiply(a, c); Multiply bd = new Multiply(b, d); Multiply abcd = new Multiply(a.plus(b), c.plus(d)); ConcurrentContext.execute(ac, bd, abcd); // Convenience method.
// a*c + ((a+b)*(c+d)-a*c-b*d) 2^n + b*d 2^2n return ac.result.plus(abcd.result.minus(ac.result.plus(bd.result)).shiftWordLeft(n)) .plus(bd.result.shiftWordLeft(n << 1)); } } private static class Multiply implements Runnable { LargeInteger left, right, result; Multiply(LargeInteger left, LargeInteger right) { this.left = left; this.right = right; } public void run() { result = left.times(right); // Recursive. } }[/code]

Concurrency can be adjusted or disabled. The default concurrency is defined by the CONCURRENCY configurable. [code] ConcurrentContext ctx = ConcurrentContext.enter(); try { ctx.setConcurrency(0); // Disables concurrency runAnalysis(); // Performs analysis sequentially. } finally { ctx.exit(); // Back to previous concurrency settings.
}[/code]

Author
Jean-Marie Dautelle
Version
6.0 December 12, 2012

Definition at line 151 of file ConcurrentContext.java.

Constructor & Destructor Documentation

◆ ConcurrentContext()

javolution.context.ConcurrentContext.ConcurrentContext ( )
protected

Default constructor.

Definition at line 181 of file ConcurrentContext.java.

181 {}

Member Function Documentation

◆ current() [1/2]

static AbstractContext javolution.context.AbstractContext.current ( )
staticinherited

Returns the current context for the current thread or

null

if this thread has no context (default).

Definition at line 61 of file AbstractContext.java.

61  {
62  return AbstractContext.CURRENT.get();
63  }

References javolution.context.AbstractContext.CURRENT.

Referenced by javolution.context.LogContext.currentLogContext(), javolution.context.SecurityContext.currentSecurityContext(), javolution.text.TextContext.currentTextContext(), javolution.xml.XMLContext.currentXMLContext(), javolution.context.LocalContext.enter(), javolution.context.ConcurrentContext.enter(), and javolution.context.LocalContext.Parameter< T >.get().

Here is the caller graph for this function:

◆ current() [2/2]

static <T extends AbstractContext> T javolution.context.AbstractContext.current ( Class< T >  type)
staticprotectedinherited

Returns the current context of specified type or

null

if none.

Definition at line 69 of file AbstractContext.java.

69  {
70  AbstractContext ctx = AbstractContext.CURRENT.get();
71  while (ctx != null) {
72  if (type.isInstance(ctx))
73  return (T) ctx;
74  ctx = ctx.outer;
75  }
76  return null;
77  }

References javolution.context.AbstractContext.CURRENT, and javolution.context.AbstractContext.outer.

◆ enter() [1/2]

static ConcurrentContext javolution.context.ConcurrentContext.enter ( )
static

Enters and returns a new concurrent context instance.

Definition at line 186 of file ConcurrentContext.java.

186  {
188  if (ctx == null) { // Root.
189  ctx = OSGiServices.getConcurrentContext();
190  }
191  return (ConcurrentContext) ctx.enterInner();
192  }

References javolution.context.AbstractContext.current(), javolution.context.AbstractContext.enterInner(), and javolution.osgi.internal.OSGiServices.getConcurrentContext().

Referenced by javolution.context.ConcurrentContext.execute(), javolution.util.internal.map.ParallelMapImpl< K, V >.perform(), javolution.util.internal.collection.ParallelCollectionImpl< E >.perform(), javolution.util.internal.map.ParallelMapImpl< K, V >.update(), and javolution.util.internal.collection.ParallelCollectionImpl< E >.update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enter() [2/2]

static <T extends AbstractContext> T javolution.context.AbstractContext.enter ( Class< T >  custom)
staticinherited

Enters the scope of a custom context. This method raises a SecurityException if the permission to enter contexts of the specified class is not granted. For example, the following disallow entering any custom context. [code] SecurityContext ctx = SecurityContext.enter(); try { ctx.revoke(new SecurityContext.Permission(AbstractContext.class, "enter")); ... // Cannot enter any custom context. } finally { ctx.exit(); // Back to previous security settings. }[/code]


Parameters
customthe custom context to enter.
Exceptions
IllegalArgumentExceptionif the specified class default constructor cannot be instantiated.
SecurityExceptionif
SecurityContext.Permission(custom, "enter")

is not granted.
See also
SecurityContext.Permission

Definition at line 101 of file AbstractContext.java.

101  {
102  SecurityContext.check(new Permission<T>(custom, "enter"));
103  try {
104  return (T) custom.newInstance().enterInner();
105  } catch (InstantiationException e) {
106  throw new IllegalArgumentException(
107  "Cannot instantiate instance of " + custom, e);
108  } catch (IllegalAccessException e) {
109  throw new IllegalArgumentException("Cannot access " + custom, e);
110  }
111  }

References javolution.context.SecurityContext.check().

Here is the call graph for this function:

◆ enterInner()

AbstractContext javolution.context.AbstractContext.enterInner ( )
protectedinherited

Enters the scope of an inner context which becomes the current context; the previous current context becomes the outer of this context.

Returns
the inner context entered.

Definition at line 141 of file AbstractContext.java.

141  {
143  inner.outer = AbstractContext.CURRENT.get();
144  AbstractContext.CURRENT.set(inner);
145  return inner;
146  }

References javolution.context.AbstractContext.CURRENT, and javolution.context.AbstractContext.outer.

Referenced by javolution.xml.XMLContext.enter(), javolution.text.TextContext.enter(), javolution.context.LogContext.enter(), javolution.context.LocalContext.enter(), javolution.context.SecurityContext.enter(), and javolution.context.ConcurrentContext.enter().

Here is the caller graph for this function:

◆ execute() [1/2]

abstract void javolution.context.ConcurrentContext.execute ( Runnable  logic)
abstract

Executes the specified logic by a concurrent thread if one available; otherwise the logic is executed by the current thread. Any exception or error occurring during the concurrent execution is propagated to the current thread upon exit of the concurrent context.

Parameters
logicthe logic to be executed concurrently when possible.

Reimplemented in javolution.context.internal.ConcurrentContextImpl.

◆ execute() [2/2]

static void javolution.context.ConcurrentContext.execute ( Runnable...  logics)
static

Convenience method to executes the specified logics concurrently. This method is equivalent to: [code] ConcurrentContext ctx = ConcurrentContext.enter(); try { ctx.execute(logics[0]); ctx.execute(logics[1]); ... } finally { ctx.exit(); }[/code]

Parameters
logicsthe logics to execute concurrently if possible.

Definition at line 209 of file ConcurrentContext.java.

209  {
210  ConcurrentContext ctx = ConcurrentContext.enter();
211  try {
212  for (Runnable logic : logics) {
213  ctx.execute(logic);
214  }
215  } finally {
216  ctx.exit();
217  }
218  }

References javolution.context.ConcurrentContext.enter(), javolution.context.ConcurrentContext.execute(), and javolution.context.ConcurrentContext.exit().

Referenced by javolution.context.ConcurrentContext.execute(), javolution.util.internal.map.ParallelMapImpl< K, V >.perform(), javolution.util.internal.collection.ParallelCollectionImpl< E >.perform(), javolution.util.internal.map.ParallelMapImpl< K, V >.update(), and javolution.util.internal.collection.ParallelCollectionImpl< E >.update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ exit()

void javolution.context.ConcurrentContext.exit ( )

Exits the scope of this concurrent context; this method blocks until all the concurrent executions are completed.

Exceptions
RuntimeExceptionre-exports any exception raised during concurrent executions.
Errorre-exports any error raised during concurrent executions.
IllegalStateExceptionif this context is not the current context.

Reimplemented from javolution.context.AbstractContext.

Reimplemented in javolution.context.internal.ConcurrentContextImpl.

Definition at line 255 of file ConcurrentContext.java.

255  { // Redefine here for documentation purpose.
256  super.exit();
257  }

Referenced by javolution.context.ConcurrentContext.execute(), javolution.util.internal.map.ParallelMapImpl< K, V >.perform(), javolution.util.internal.collection.ParallelCollectionImpl< E >.perform(), javolution.util.internal.map.ParallelMapImpl< K, V >.update(), and javolution.util.internal.collection.ParallelCollectionImpl< E >.update().

Here is the caller graph for this function:

◆ getConcurrency()

abstract int javolution.context.ConcurrentContext.getConcurrency ( )
abstract

Returns the current concurrency which is basically the number of concurrent threads authorized to do concurrent work (on top of all others threads of course).

Reimplemented in javolution.context.internal.ConcurrentContextImpl.

Referenced by javolution.util.internal.map.ParallelMapImpl< K, V >.perform(), javolution.util.internal.collection.ParallelCollectionImpl< E >.perform(), javolution.util.internal.map.ParallelMapImpl< K, V >.update(), and javolution.util.internal.collection.ParallelCollectionImpl< E >.update().

Here is the caller graph for this function:

◆ getOuter()

AbstractContext javolution.context.AbstractContext.getOuter ( )
protectedinherited

Returns the outer context of this context or

null

if this context has no outer context.

Definition at line 167 of file AbstractContext.java.

167  {
168  return outer;
169  }

◆ inherit()

static void javolution.context.AbstractContext.inherit ( AbstractContext  ctx)
staticinherited

Inherits the specified context which becomes the context of the current thread. This method is particularly useful when creating new threads to make them inherits from the context stack of the parent thread. [code] //Spawns a new thread inheriting the context of the current thread. MyThread myThread = new MyThread(); myThread.inherited = AbstractContext.current(); myThread.start(); ... class MyThread extends Thread { AbstractContext inherited; public void run() { AbstractContext.inherit(inherited); // Sets current context. ... } }[/code]

Definition at line 131 of file AbstractContext.java.

131  {
132  CURRENT.set(ctx);
133  }

Referenced by javolution.context.internal.ConcurrentThreadImpl.run().

Here is the caller graph for this function:

◆ inner()

abstract AbstractContext javolution.context.AbstractContext.inner ( )
abstractprotectedinherited

Returns a new inner instance of this context inheriting the properties of this context. The new instance can be configured independently from its parent.

Reimplemented in javolution.context.internal.ConcurrentContextImpl, javolution.text.internal.TextContextImpl, javolution.context.internal.LogContextImpl, javolution.context.internal.SecurityContextImpl, javolution.xml.internal.XMLContextImpl, and javolution.context.internal.LocalContextImpl.

◆ setConcurrency()

abstract void javolution.context.ConcurrentContext.setConcurrency ( int  concurrency)
abstract

Sets the maximum concurrency. Setting a value greater than the current concurrency has no effect (concurrency can only be reduced).

Reimplemented in javolution.context.internal.ConcurrentContextImpl.

Member Data Documentation

◆ CONCURRENCY

final Configurable<Integer> javolution.context.ConcurrentContext.CONCURRENCY
static
Initial value:
= new Configurable<Integer>() {
@Override
protected Integer getDefault() {
return Runtime.getRuntime().availableProcessors() - 1;
}
@Override
protected Integer initialized(Integer value) {
return MathLib.min(value, 65536);
}
@Override
protected Integer reconfigured(Integer oldCount, Integer newCount) {
throw new UnsupportedOperationException(
"Concurrency reconfiguration not supported.");
}
}

Holds the maximum concurrency
(default Runtime.getRuntime().availableProcessors() - 1). The maximum concurrency is configurable. For example, the JVM option

-Djavolution.context.ConcurrentContext#CONCURRENCY=0

disables concurrency.

Definition at line 160 of file ConcurrentContext.java.

Referenced by javolution.context.internal.ConcurrentContextImpl.ConcurrentContextImpl().

◆ CURRENT

final ThreadLocal<AbstractContext> javolution.context.AbstractContext.CURRENT = new ThreadLocal<AbstractContext>()
staticprivateinherited

◆ outer

AbstractContext javolution.context.AbstractContext.outer
privateinherited

Holds the outer context or

null

if none (top context).

Definition at line 50 of file AbstractContext.java.

Referenced by javolution.context.AbstractContext.current(), and javolution.context.AbstractContext.enterInner().


The documentation for this class was generated from the following file:
javolution.context.AbstractContext.CURRENT
static final ThreadLocal< AbstractContext > CURRENT
Definition: AbstractContext.java:45
javolution.context.AbstractContext.inner
abstract AbstractContext inner()
javolution.context.ConcurrentContext.ConcurrentContext
ConcurrentContext()
Definition: ConcurrentContext.java:181
javolution.context.AbstractContext.outer
AbstractContext outer
Definition: AbstractContext.java:50
javolution.context.ConcurrentContext.CONCURRENCY
static final Configurable< Integer > CONCURRENCY
Definition: ConcurrentContext.java:160
javolution.context.AbstractContext.current
static AbstractContext current()
Definition: AbstractContext.java:61
javolution.context.AbstractContext.AbstractContext
AbstractContext()
Definition: AbstractContext.java:55