javolution.util

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 » Development » Javolution » javolution.util 
javolution.util

Provides high-performance collection classes and miscellaneous utilities; although this package provides very few collection classes, they are substitutes for most of java.util.* classes (for example, java.util.IdentityHashMap would be a {@link javolution.util.FastMap FastMap} with an {@link javolution.util.FastComparator#IDENTITY identity} key comparator).

Overview:

Javolution collections are compliant with standard collections (generic when built with the ant target 1.5) and they can safely be used with RTSJ virtual machines (e.g. if the capacity of a collection increases, the extension part is allocated from the same memory area as the collection itself).

They support direct iterations with the following advantages:

  • Faster than iterators, see benchmark.
  • No object creation not even the iterator object itself. For example, visiting a tree structure using iterators creates as many iterators as they are nodes in the tree:[code] public static void visit(Collection node) { for (Collection i : node) { // Creates iterator. visit(i); } }[/code] Not so with direct iterations:[code] public static void visit(FastCollection node) { for (FastCollection.Record r = node.head(), end = node.tail(); (r = r.getNext()) != end;) { visit(node.valueOf(r)); } }[/code]
  • Used to implement most of {@link javolution.util.FastCollection FastCollection} base class methods (including {@link javolution.util.FastCollection#iterator iterator()}).
  • Support forward/backward iterations from the start (head) or from the end (tail)
  • Thread-safe as long as objects are not inserted/removed during iterations. Objects can safely be append/prepend by the current thread or other threads. (Note: {@link javolution.util.FastMap#setShared Shared FastMap} are always thread-safe even when entries are removed).
  • Fully integrated with the JDK1.5+ generic framework (strong typing) and still compatible with other platforms (J2ME, 1.4, GCJ).
Here are few examples of direct iterations:[code] FastList list; for (FastList.Node n = list.head(), end = list.tail(); (n = n.getNext()) != end;) { String value = n.getValue(); // No typecast necessary. } ... FastMap map; for (FastMap.Entry e = map.head(), end = map.tail(); (e = e.getNext()) != end;) { String key = e.getKey(); // No typecast necessary. Thread value = e.getValue(); // No typecast necessary. }[/code]

Users may provide a read-only view of any {@link javolution.util.FastCollection FastCollection} (or {@link javolution.util.FastMap FastMap}) instance using the {@link javolution.util.FastCollection#unmodifiable() FastCollection.unmodifiable()} (or {@link javolution.util.FastMap#unmodifiable FastMap.unmodifiable()}) method. For example:[code] public class Polynomial { private final FastSet _terms = new FastSet(); // Read-only view (also thread-safe as terms are not "deleted"). public Set getTerms() { return _terms.unmodifiable(); } }[/code]

Collection/maps of primitive types can be created using the {@link javolution.util.Index Index} class. It avoids the overhead of wrapping primitives types (for reasonably small int values). For example:[code] public class SparseVector { FastMap _elements = new FastMap(); ... }[/code]

Although all collections capacity increases smoothly (no resizing/copy or rehashing ever performed), it is nevertheless possible to specify an initial capacity; in which case, all necessary storage is allocated at creation. For RTSJ VMs, all collections/maps can reside in ImmortalMemory (e.g. static) and be used by all threads (including NoHeapRealtimeThread) without resulting into memory leaks or illegal access errors. For example:[code] public class XmlFormat { // RTSJ Unsafe! Memory leaks (when entries removed) or IllegalAssignmentError (when new entries while in ScopedArea). static HashMap ClassToFormat = HashMap(); // RTSJ Safe! Removed entries are internally recycled, new entries are in ImmortalMemory. static FastMap ClassToFormat = FastMap(); }[/code] For more details, please read Javolution-Collection.pdf

.

Temporary collection classes can be recycled (e.g. throw-away collections) to avoid the creation cost. For example:[code] static void removeDuplicate(List persons) { FastSet tmp = FastSet.newInstance(); // Possibly recycled instance. tmp.addAll(persons); persons.clear(); persons.addAll(tmp); FastSet.recycle(tmp); // Recycles the temporary instance. }[/code]

Here is a summary of the collection classes with their defining characteristics:
Javolution Collections Classes
Ordering Duplication Allowed Custom Comparators Record Type Miscellaneous
{@link javolution.util.FastTable FastTable} Insertion Order Yes {@link javolution.util.FastTable#setValueComparator setValueComparator(FastComparator)} {@link javolution.util.Index Index} Thread-safe random access collection
No array resize/copy ever performed
{@link javolution.util.FastList FastList} Insertion Order Yes {@link javolution.util.FastList#setValueComparator setValueComparator(FastComparator)} {@link javolution.util.FastList.Node Node} Recycle their own nodes (no adverse effect on GC)
{@link javolution.util.FastSet FastSet} Insertion Order No {@link javolution.util.FastSet#setValueComparator setValueComparator(FastComparator)} {@link javolution.util.FastCollection.Record Record} Based on {@link javolution.util.FastSet FastMap} (same characteristics)
FastTree Comparator No setValueComparator(FastComparator) TreeNode (not implemented)
{@link javolution.util.FastMap FastMap} Insertion Order Key: No
Value: Yes
{@link javolution.util.FastMap#setKeyComparator setKeyComparator(FastComparator)}
{@link javolution.util.FastMap#setValueComparator setValueComparator(FastComparator)}
{@link javolution.util.FastMap.Entry Entry} Thread-safe when marked as {@link javolution.util.FastMap#setShared shared}
No rehash/resize ever performed
Recycle their own entries (no adverse effect on GC)

FAQ:

  1. ArrayList may throw ConcurrentModificationException, but Javolution FastTable does not, why?

    FastTable (or any Javolution collection/map) do support concurrent modifications as long as these are not insertions at an arbitrary position or deletions (Note: Shared FastMap does support concurrent deletions). In other words you can safely iterate (using iterators or not) through a FastList, FastMap (entries, keys values), FastTable, etc. while new elements/entries are being added (by you or another thread). You can also export a {@link javolution.util.FastCollection#unmodifiable() read-only} view over your collection and still add more elements to it.

    Disallowing concurrent modifications (standard java util) has proven to be a performance killer for many (forcing users to work with copies of their whole collections). Furthermore the additional checks required directly impact performance (e.g. ArrayList iterations about 3x slower than FastTable iterations).

  2. Do you have a test case showing any scenario of concurrent modification where ArrayList "fails" and FastTable doesn't?

    Let's say that you have a collection of "Units", and you want to provide users with a read-only view of these units. The following code will fail miserably:[code] public class Unit { static ArrayList INSTANCES = new ArrayList(); public static List getInstances() { return Collections.unmodifiableList(INSTANCES); } }[/code] Why? Because, it the user iterates on the read-only list of units while a new unit is added to the collection (by another thread) a ConcurrentModificationException is automatically raised. In other words, it is almost impossible to provide a "read-only" view of non-fixed size collections with the current java.util classes (e.g. you will have to replace the whole collection each time a new unit is added).

    Now with FastTable the following is completely safe even when new units are added:[code] public class Unit { static FastTable INSTANCES = new FastTable(); public static List getInstances() { return INSTANCES.unmodifiable(); } }[/code]

  3. Do checks for concurrent modifications make your code safer?

    Not really. The current checks for concurrent modifications do not "guarantee" that concurrent modifications will not occur! You can imagine two threads one updating a collection and the other one iterating the collection. As long as the update is not performed while the other thread is iterating, everything is fine (no ConcurrentModificationException)! But, if for a reason or another the timing changes (e.g. in the user environment) and iterations are performed at the wrong time then your application crashes... Not a good thing and very high probability for this to happen!

  4. Are {@link javolution.util.FastMap#setShared shared maps} valid substitutes for ConcurrentHashMap?

    Unlike ConcurrentHashMap access to a shared FastMap never blocks. Retrieval reflects the map state not older than the last time the accessing threads have been synchronized* (for multi-processors systems synchronizing ensures that the CPU internal cache is not stale).

    In practice, it means that most well-written concurrent programs should be able to use shared FastMap in place of ConcurrentHashMap as threads are already synchronized to ensure proper behavior.

    * It is important for both threads to synchronize on the same monitor in order to set up the happens-before relationship properly. It is not the case that everything visible to thread A when it synchronizes on object X becomes visible to thread B after it synchronizes on object Y. The release and acquire have to "match" (i.e., be performed on the same monitor) to have the right semantics. Otherwise, the code has a data race.

  5. Are all Javolution collection thread-safe?

    Collections/Maps are thread-safe with regard to access (no need to synchronize reading even if the collection is modified concurrently). But the modifications themselves require either the collection/map to be marked shared or synchronization to be used.

  6. What is the overhead in term of performance when FastMap.setShared is set to true?

    Marking the map shared avoid synchronizing when possible (e.g. put when entry already exists or remove when entry does not exist), if a new entry is created and added, synchronization is performed internally. In all cases there is no impact on reading (never synchronized).

Java Source File NameTypeComment
FastCollection.javaClass

This class represents collections which can quickly be iterated over (forward or backward) in a thread-safe manner without creating new objects and without using FastCollection.iterator iterators .

FastComparator.javaClass

This class represents a comparator to be used for equality as well as for ordering; instances of this class provide a hashcode function consistent with equal (if two objects FastComparator.areEqualare equal , they have the same FastComparator.hashCodeOf hashcode ), equality with null values is supported.

FastComparator can be employed with FastMap (e.g.

FastIterator.javaClass
FastList.javaClass

This class represents a linked list with real-time behavior; smooth capacity increase and no memory allocation as long as the list size does not exceed its initial capacity.

All of the operations perform as could be expected for a doubly-linked list ( FastList.addLast insertion / FastList.removeLast() deletion at the end of the list are nonetheless the fastest).

FastMap.javaClass

This class represents a hash map with real-time behavior; smooth capacity increase and thread-safe without external synchronization when FastMap.isShared shared .

FastMap has a predictable iteration order, which is the order in which keys are inserted into the map (similar to java.util.LinkedHashMap collection class).

FastSet.javaClass

This class represents a set collection backed by a FastMap ; smooth capacity increase and no rehashing ever performed.

FastSet , as for any FastCollection sub-class, supports thread-safe fast iterations without using iterators.

FastTable.javaClass

This class represents a random access collection with real-time behavior (smooth capacity increase).

This class has the following advantages over the widely used java.util.ArrayList:

  • No large array allocation (for large collections multi-dimensional arrays are employed).
Index.javaClass

This class represents a unique index which can be used instead of java.lang.Integer for primitive data types collections.

LocalMap.javaClass

This class represents a map which can be temporarily modified without impacting other threads ( LocalContext locally scoped changes).

Operation on instances of this class are completely thread-safe.

ReentrantLock.javaClass

This class represents a reentrant lock with the same semantics as built-in Java synchronized locks: Once a thread has a lock, it can re-obtain it any number of times without blocking.

StandardLog.javaClass

This class represents a specialized logging context forwarding events to a standard logger (java.util.logging.Logger).

This class leverages the capabilities of the standard logging facility and extends it to support specialized LogContext logging on a thread or object basis.

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