Javolution 6.0.0 java
|
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< AbstractContext > | CURRENT = new ThreadLocal<AbstractContext>() |
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]
Definition at line 151 of file ConcurrentContext.java.
|
protected |
|
staticinherited |
Returns the current context for the current thread or
if this thread has no context (default).
Definition at line 61 of file AbstractContext.java.
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().
|
staticprotectedinherited |
Returns the current context of specified type or
if none.
Definition at line 69 of file AbstractContext.java.
References javolution.context.AbstractContext.CURRENT, and javolution.context.AbstractContext.outer.
|
static |
Enters and returns a new concurrent context instance.
Definition at line 186 of file ConcurrentContext.java.
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().
|
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]
custom | the custom context to enter. |
IllegalArgumentException | if the specified class default constructor cannot be instantiated. |
SecurityException | if SecurityContext.Permission(custom, "enter")
is not granted. |
Definition at line 101 of file AbstractContext.java.
References javolution.context.SecurityContext.check().
|
protectedinherited |
Enters the scope of an inner context which becomes the current context; the previous current context becomes the outer of this context.
Definition at line 141 of file AbstractContext.java.
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().
|
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.
logic | the logic to be executed concurrently when possible. |
Reimplemented in javolution.context.internal.ConcurrentContextImpl.
|
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]
logics | the logics to execute concurrently if possible. |
Definition at line 209 of file ConcurrentContext.java.
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().
void javolution.context.ConcurrentContext.exit | ( | ) |
Exits the scope of this concurrent context; this method blocks until all the concurrent executions are completed.
RuntimeException | re-exports any exception raised during concurrent executions. |
Error | re-exports any error raised during concurrent executions. |
IllegalStateException | if 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.
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().
|
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().
|
protectedinherited |
Returns the outer context of this context or
if this context has no outer context.
Definition at line 167 of file AbstractContext.java.
|
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.
Referenced by javolution.context.internal.ConcurrentThreadImpl.run().
|
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.
|
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.
|
static |
Holds the maximum concurrency
(default Runtime.getRuntime().availableProcessors() - 1
). The maximum concurrency is configurable. For example, the JVM option
disables concurrency.
Definition at line 160 of file ConcurrentContext.java.
Referenced by javolution.context.internal.ConcurrentContextImpl.ConcurrentContextImpl().
|
staticprivateinherited |
Holds the last context entered (thread-local).
Definition at line 45 of file AbstractContext.java.
Referenced by javolution.context.AbstractContext.current(), javolution.context.AbstractContext.enterInner(), and javolution.context.AbstractContext.exit().
|
privateinherited |
Holds the outer context or
if none (top context).
Definition at line 50 of file AbstractContext.java.
Referenced by javolution.context.AbstractContext.current(), and javolution.context.AbstractContext.enterInner().