Javolution 6.0.0 java
javolution.context.internal.ConcurrentContextImpl Class Reference
Inheritance diagram for javolution.context.internal.ConcurrentContextImpl:
[legend]
Collaboration diagram for javolution.context.internal.ConcurrentContextImpl:
[legend]

Public Member Functions

 ConcurrentContextImpl ()
 
 ConcurrentContextImpl (ConcurrentContextImpl parent)
 
synchronized void completed (Throwable error)
 
void execute (Runnable logic)
 
synchronized void exit ()
 
int getConcurrency ()
 
void setConcurrency (int concurrency)
 

Static Public Member Functions

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

Static Public Attributes

static final Configurable< Integer > CONCURRENCY
 

Protected Member Functions

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

Static Protected Member Functions

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

Private Attributes

int completedCount
 
Throwable error
 
int initiatedCount
 
final ConcurrentContextImpl parent
 
ConcurrentThreadImpl[] threads
 
AbstractContext outer
 

Static Private Attributes

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

Detailed Description

Holds the default implementation of ConcurrentContext.

Definition at line 17 of file ConcurrentContextImpl.java.

Constructor & Destructor Documentation

◆ ConcurrentContextImpl() [1/2]

javolution.context.internal.ConcurrentContextImpl.ConcurrentContextImpl ( )

Default constructor (root).

Definition at line 29 of file ConcurrentContextImpl.java.

29  {
30  this.parent = null;
31  int nbThreads = ConcurrentContext.CONCURRENCY.get();
32  threads = new ConcurrentThreadImpl[nbThreads];
33  for (int i = 0; i < nbThreads; i++) {
34  threads[i] = new ConcurrentThreadImpl();
35  threads[i].start();
36  }
37  }

References javolution.context.ConcurrentContext.CONCURRENCY, javolution.lang.Configurable< T >.get(), and javolution.context.internal.ConcurrentContextImpl.threads.

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

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

◆ ConcurrentContextImpl() [2/2]

javolution.context.internal.ConcurrentContextImpl.ConcurrentContextImpl ( ConcurrentContextImpl  parent)

Inner implementation.

Definition at line 42 of file ConcurrentContextImpl.java.

42  {
43  this.parent = parent;
44  this.threads = parent.threads; // Inherit threads from parents.
45  }

References javolution.context.internal.ConcurrentContextImpl.parent, and javolution.context.internal.ConcurrentContextImpl.threads.

Member Function Documentation

◆ completed()

synchronized void javolution.context.internal.ConcurrentContextImpl.completed ( Throwable  error)

Definition at line 48 of file ConcurrentContextImpl.java.

48  {
49  if (error != null) {
50  this.error = error;
51  }
53  this.notify();
54  }

References javolution.context.internal.ConcurrentContextImpl.completedCount, and javolution.context.internal.ConcurrentContextImpl.error.

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

Here is the caller graph for this function:

◆ 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 ( )
staticinherited

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]

void javolution.context.internal.ConcurrentContextImpl.execute ( Runnable  logic)

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 from javolution.context.ConcurrentContext.

Definition at line 57 of file ConcurrentContextImpl.java.

57  {
58  // Find a thread not busy.
59  for (ConcurrentThreadImpl thread : threads) {
60  if (thread.execute(logic, this)) {
62  return;
63  }
64  }
65  // Executes by current thread.
66  try {
67  logic.run();
68  } catch (Throwable e) {
69  error = e;
70  }
71  }

References javolution.context.internal.ConcurrentContextImpl.error, javolution.context.internal.ConcurrentContextImpl.initiatedCount, and javolution.context.internal.ConcurrentContextImpl.threads.

◆ execute() [2/2]

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

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()

synchronized void javolution.context.internal.ConcurrentContextImpl.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.ConcurrentContext.

Definition at line 74 of file ConcurrentContextImpl.java.

74  {
75  super.exit();
76  try {
77  while (initiatedCount != completedCount) {
78  this.wait();
79  }
80  } catch (InterruptedException ex) {
81  this.error = ex;
82  }
83  if (error == null)
84  return; // Everything fine.
85  if (error instanceof RuntimeException)
86  throw (RuntimeException) error;
87  if (error instanceof Error)
88  throw (Error) error;
89  throw new RuntimeException(error);
90  }

References javolution.context.internal.ConcurrentContextImpl.completedCount, javolution.context.internal.ConcurrentContextImpl.error, and javolution.context.internal.ConcurrentContextImpl.initiatedCount.

◆ getConcurrency()

int javolution.context.internal.ConcurrentContextImpl.getConcurrency ( )

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 from javolution.context.ConcurrentContext.

Definition at line 93 of file ConcurrentContextImpl.java.

93  {
94  return threads.length;
95  }

References javolution.context.internal.ConcurrentContextImpl.threads.

◆ 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()

ConcurrentContext javolution.context.internal.ConcurrentContextImpl.inner ( )
protected

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 from javolution.context.AbstractContext.

Definition at line 109 of file ConcurrentContextImpl.java.

109  {
110  return new ConcurrentContextImpl(this);
111  }

References javolution.context.internal.ConcurrentContextImpl.ConcurrentContextImpl().

Here is the call graph for this function:

◆ setConcurrency()

void javolution.context.internal.ConcurrentContextImpl.setConcurrency ( int  concurrency)

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

Reimplemented from javolution.context.ConcurrentContext.

Definition at line 98 of file ConcurrentContextImpl.java.

98  {
99  // The setting of the concurrency can only reduce the number
100  // of threads available in the context.
101  int nbThreads = MathLib.min(parent.threads.length, concurrency);
102  threads = new ConcurrentThreadImpl[nbThreads];
103  for (int i = 0; i < nbThreads; i++) { // Reused from parent threads.
104  threads[i] = parent.threads[i];
105  }
106  }

References javolution.lang.MathLib.min(), javolution.context.internal.ConcurrentContextImpl.parent, and javolution.context.internal.ConcurrentContextImpl.threads.

Here is the call graph for this function:

Member Data Documentation

◆ completedCount

int javolution.context.internal.ConcurrentContextImpl.completedCount
private

◆ CONCURRENCY

final Configurable<Integer> javolution.context.ConcurrentContext.CONCURRENCY
staticinherited
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

◆ error

◆ initiatedCount

int javolution.context.internal.ConcurrentContextImpl.initiatedCount
private

◆ 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().

◆ parent

◆ threads


The documentation for this class was generated from the following file:
javolution.context.internal.ConcurrentContextImpl.ConcurrentContextImpl
ConcurrentContextImpl()
Definition: ConcurrentContextImpl.java:29
javolution.context.internal.ConcurrentContextImpl.completedCount
int completedCount
Definition: ConcurrentContextImpl.java:19
javolution.context.internal.ConcurrentContextImpl.threads
ConcurrentThreadImpl[] threads
Definition: ConcurrentContextImpl.java:24
javolution.context.internal.ConcurrentContextImpl.error
Throwable error
Definition: ConcurrentContextImpl.java:20
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.internal.ConcurrentContextImpl.initiatedCount
int initiatedCount
Definition: ConcurrentContextImpl.java:22
javolution.context.internal.ConcurrentContextImpl.parent
final ConcurrentContextImpl parent
Definition: ConcurrentContextImpl.java:23
javolution.context.AbstractContext.AbstractContext
AbstractContext()
Definition: AbstractContext.java:55