Java Doc for ConcurrentContext.java in  » Science » javolution-5.2 » javolution » context » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » javolution 5.2 » javolution.context 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   javolution.context.Context
      javolution.context.ConcurrentContext

ConcurrentContext
abstract public class ConcurrentContext extends Context (Code)

This class represents a context 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 ConcurrentContext.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 Javolution Configuration for details).

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. For example, if the main thread runs in a StackContext , concurrent executions are performed in the same StackContext as well.

Concurrent contexts are easy to use, and provide automatic load-balancing between processors with almost no overhead. Here is an example of concurrent/recursive implementation of the Karatsuba 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 = Multiply.valueOf(a, c); Multiply bd = Multiply.valueOf(b, d); Multiply abcd = Multiply.valueOf(a.plus(b), c.plus(d)); ConcurrentContext.enter(); try { ConcurrentContext.execute(ac); ConcurrentContext.execute(bd); ConcurrentContext.execute(abcd); } finally { ConcurrentContext.exit(); // Waits for all concurrent threads to complete. } // a*c + ((a+b)*(c+d)-a*c-b*d) 2^n + b*d 2^2n return ac.value().plus( abcd.value().minus(ac.value().plus(bd.value())).shiftWordLeft(n)).plus( bd.value().shiftWordLeft(n << 1)); } } private static class Multiply implements Runnable { LargeInteger _left, _right, _value; static Multiply valueOf(LargeInteger left, LargeInteger right) { Multiply multiply = new Multiply(); // Or use an ObjectFactory (to allow stack allocation). multiply._left = left; multiply._right = right; return multiply; } public void run() { _value = _left.times(_right); // Recursive. } public LargeInteger value() { return _result; } };[/code] Here is a concurrent/recursive quick/merge sort using anonymous inner classes (the same method is used for benchmark):[code] private void quickSort(final FastTable 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 t1 = FastTable.newInstance(); final FastTable t2 = FastTable.newInstance(); ConcurrentContext.enter(); try { ConcurrentContext.execute(new Runnable() { public void run() { t1.addAll(table.subList(0, size / 2)); quickSort(t1); // Recursive. } }); ConcurrentContext.execute(new Runnable() { public void run() { t2.addAll(table.subList(size / 2, size)); quickSort(t2); // Recursive. } }); } finally { ConcurrentContext.exit(); } // 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++; } } } FastTable.recycle(t1); FastTable.recycle(t2); } }[/code]

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

ConcurrentContext.getConcurrency() Concurrency can be LocalContext locally adjusted. For example:[code] LocalContext.enter(); try { // Do not use more than half of the processors during analysis. ConcurrentContext.setConcurrency((Runtime.getRuntime().availableProcessors() / 2) - 1); runAnalysis(); // Use concurrent contexts internally. } finally { LocalContext.exit(); }[/code]

It should be noted that the concurrency cannot be increased above the configurable ConcurrentContext.MAXIMUM_CONCURRENCY maximum concurrency . In other words, if the maximum concurrency is 0, concurrency is disabled regardless of local concurrency settings.


author:
   Jean-Marie Dautelle
version:
   5.1, July 2, 2007

Inner Class :final static class Default extends ConcurrentContext

Field Summary
final public static  ConfigurableDEFAULT
     Holds the default implementation.
final public static  ConfigurableMAXIMUM_CONCURRENCY
     Holds the maximum number of concurrent executors (see Javolution Configuration for details).

Constructor Summary
protected  ConcurrentContext()
     Default constructor.

Method Summary
public static  ConcurrentContextenter()
     Enters a concurrent context (instance of ConcurrentContext.DEFAULT ).
public static  voidexecute(Runnable logic)
     Executes the specified logic by a concurrent thread if one available; otherwise the logic is executed by the current thread.
abstract protected  voidexecuteAction(Runnable logic)
     Executes the specified logic concurrently if possible.
public static  Contextexit()
     Exits the current concurrent context.
public static  intgetConcurrency()
     Returns the LocalContext local concurrency.
public static  voidsetConcurrency(int concurrency)
     Set the LocalContext local concurrency.

Field Detail
DEFAULT
final public static Configurable DEFAULT(Code)
Holds the default implementation. Concurrent executions are performed in the same memory area and at the same priority as the calling thread. This implementation uses javax.realtime.RealtimeThread for concurrent threads. Alternative (RTSJ) implementations could also use javax.realtime.NoHeapRealtimeThread.



MAXIMUM_CONCURRENCY
final public static Configurable MAXIMUM_CONCURRENCY(Code)
Holds the maximum number of concurrent executors (see Javolution Configuration for details).




Constructor Detail
ConcurrentContext
protected ConcurrentContext()(Code)
Default constructor.




Method Detail
enter
public static ConcurrentContext enter()(Code)
Enters a concurrent context (instance of ConcurrentContext.DEFAULT ). the concurrent context being entered.



execute
public static void execute(Runnable logic)(Code)
Executes the specified logic by a concurrent thread if one available; otherwise the logic is executed by the current thread. Any exception or error occuring during concurrent executions is propagated to the current thread upon ConcurrentContext.exit of the concurrent context.
Parameters:
  logic - the logic to execute concurrently if possible.
throws:
  ClassCastException - if the current context is not aConcurrentContext.



executeAction
abstract protected void executeAction(Runnable logic)(Code)
Executes the specified logic concurrently if possible.
Parameters:
  logic - the logic to execute.



exit
public static Context exit()(Code)
Exits the current concurrent context. the concurrent context being exited.
throws:
  ClassCastException - if the context is not a concurrent context.



getConcurrency
public static int getConcurrency()(Code)
Returns the LocalContext local concurrency. the maximum number of concurrent thread.



setConcurrency
public static void setConcurrency(int concurrency)(Code)
Set the LocalContext local concurrency. Concurrency is hard limited by ConcurrentContext.MAXIMUM_CONCURRENCY .
Parameters:
  concurrency - the new concurrency (0 or negativenumber to disable concurrency).



Fields inherited from javolution.context.Context
final public static Context ROOT(Code)(Java Doc)

Methods inherited from javolution.context.Context
final public static Context enter(Context context)(Code)(Java Doc)
final public static Context enter(Class contextType)(Code)(Java Doc)
abstract protected void enterAction()(Code)(Java Doc)
public static Context exit()(Code)(Java Doc)
final public static void exit(Context ctx)(Code)(Java Doc)
abstract protected void exitAction()(Code)(Java Doc)
final AllocatorContext getAllocatorContext()(Code)(Java Doc)
public static Context getCurrent()(Code)(Java Doc)
final public Context getOuter()(Code)(Java Doc)
final public Thread getOwner()(Code)(Java Doc)
protected static void setCurrent(ConcurrentContext context)(Code)(Java Doc)
public String toString()(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.