public final class Concurrency extends Object implements AutoCloseable
ConcurrentContext
usage by using the the
Java 'try' for resources capability.
Normally you will write
ConcurrentContext.enter();
try {
ConcurrentContext.execute(task1);
ConcurrentContext.execute(task2); }
} finally {
ConcurrentContext.exit();
}
try (Concurrency c = Concurrency.start()) {
c.execute(task1);
c.execute(task2);
}
public class Main {
public static void main(final String[] args) {
// Using 10 threads for evolving.
Concurrency.setConcurrency(9);
// Forces the application only to use one thread.
Concurrency.setConcurrency(0);
}
}
ConcurrentContext
from the JScience project uses it's
own--optimized--thread-pool implementation. If you need to have a single
executor service, for the GA and your own classes, you can initialize the
Concurrency
class with the ForkJoinPool
from the JDK.
public class Main {
public static void main(final String[] args) {
final int nthreads = 10;
final ForkJoinPool pool = new ForkJoinPool(nthreads);
Concurrency.setForkJoinPool(pool);
...
}
}
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
execute(Runnable task) |
static Class<ConcurrentContext> |
getContext() |
static void |
setConcurrency(int concurrency)
Set the number of threads to use by the
ConcurrentContext . |
static void |
setContext(Class<? extends ConcurrentContext> type)
Set the concurrent-context to be used by the concurrency.
|
static void |
setDefaultContext()
Reset to the default default context.
|
static void |
setForkJoinPool(ForkJoinPool pool)
Convenience method for setting the
ForkJoinPool and the concurrent
context to ForkJoinContext . |
static Concurrency |
start() |
public static void setConcurrency(int concurrency)
ConcurrentContext
.concurrency
- the number of threads to use for the default concurrent
context.public static void setContext(Class<? extends ConcurrentContext> type)
type
- the concurrent-context type.NullPointerException
- if the given type
is null
.public static void setForkJoinPool(ForkJoinPool pool)
ForkJoinPool
and the concurrent
context to ForkJoinContext
.pool
- the ForkJoinPool
to use for concurrency.public static void setDefaultContext()
public static Class<ConcurrentContext> getContext()
public static Concurrency start()
public void close()
close
in interface AutoCloseable
© 2007-2014 Franz Wilhelmstötter (2014-03-07 19:35)