hash-map which supports full concurrency for read operations and
adjustable expected concurrency for write operations.
takes an integer argument concurrencyLevel in the constructor which
specifies how many concurrent write operations are expected to occur
for the hash-map. The hash-map then tries to allow that many write
threads to execute concurrently by internally partitioning the table
into that many partitions assuming that a single write thread will
use a different partition. However, that is not guaranteed because
all threads may want to update a single partition at the same time.
Hence this write concurrency is a best effort implementation.
and Collections.synchronizedMap (new HashMap ())
is concurrent while other one is synchronized.
means that it blocks other threads if a single thread is reading or
is a much more efficient version which uses ReadWriteLocks to allow
concurrent reads and and adjustable concurrent writes. Also, it uses
Atomic variables (explained 3-4 pages below) which use
Compare-And-Swap technique which is much more efficient than blocking
all concurrent data structures in the concurrent package are much
more efficient than their synchronous counterparts.
key difference between the historical collection classes and the new
implementations within the Collections Framework is the new classes
thread-safe. This was done such that if you don't need the
synchronization, you don't use it, and everything works much faster.
If, however, you are using a collection in a multithreaded
environment, where multiple threads can modify the collection
simultaneously, the modifications need to be synchronized. The
Collections class provides for the the ability to wrap existing
collections into synchronized ones with another set of six methods:
when making a collection read-only, you synchronize the collection
immediately after creating it. You also must make sure you do not
retain a reference to the original collection, or else you can access
the collection unsynchronized. The simplest way to make sure you
don't retain a reference is to never create one:
set = Collection.synchronizedSet(new HashSet());
a collection unmodifiable also makes a collection thread-safe, as the
collection can't be modified. This avoids the synchronization
are implemented much like the Collections.unmodifiableList() methods.
When synchronizedList() is called, it creates a wrapper class. All
functions of this wrapper class are synchronized and each function
simply calls the original collection’s function.
the wrapper class implements the corresponding collection’s
interface and makes it synchronized too.
this means that the resulting collection is very heavily synchronized
and can become a performance bottleneck.