Since I have looked last (a while ago, may be java 3) there are lot of enhancements to concurrency in Java. I needed a stateful java object that gets updated once in a while but gets read a lot from multiple threads on a tomcat server. I could use the synchronized methods on that data object but that would lock the object both for reads and writes.

So I was looking to see if there are any native structures in Java that can do this more efficiently. I am of the kind that will not look if I don?t need something. Good and bad.

So this page is how that research went, looking for answers for efficient reads. I will summarize the research quickly in this introductory statement and the rest you can see in the rest of the document.

Quick Summary

There is a LOT of concurrency support in Java now!

Starting around Java 5, lot of things got added. Some of these features include at a high level

Volatile - to sync memory reads between threads

ReadWriteLock - to enhance when reads are more often than write

StampedLock - enhancement on ReadWriteLock to provide for optimistic reads

Then there are specific type of collection objects that are already coded correctly to give some of the needed behavior out of the box. These include

ConcurrentHashMap - Faster reads and writes. Yet consistent. Uses over 16 locks underneath.

CopyOnWriteArrayList - To speedup frequent reads by not locking the dataset at all

These improvements right in the base concurrency model, and are not withstanding such functional aspects of the new java such as streams, lambda functions, thread executors, futures etc. I then ran into such external efforts such as Akka (actor based programming), and Guava (Googles core java libraries).

As I have suspected, but realizing now, for concurrency and others, Java is a new animal, (probably for a while), now! It seem to have shed its skin of simplicity :)

satya - 6/13/2017, 6:15:11 PM

Concurrency and hash maps in java

Concurrency and hash maps in java

Search for: Concurrency and hash maps in java

satya - 6/13/2017, 6:16:12 PM

Does insert and delete impact a find operation on hash maps

Does insert and delete impact a find operation on hash maps

Search for: Does insert and delete impact a find operation on hash maps

When the delete, create, and find are on different primary keys

satya - 6/13/2017, 6:17:01 PM

Frequent reads an Concurrency pattern

Frequent reads an Concurrency pattern

Search for: Frequent reads an Concurrency pattern

satya - 6/14/2017, 9:25:18 AM

Java structure with no read locks but synchronized writes

Java structure with no read locks but synchronized writes

Search for: Java structure with no read locks but synchronized writes

satya - 6/14/2017, 9:25:41 AM

How to use ReadwriteLock in Java

How to use ReadwriteLock in Java

Search for: How to use ReadwriteLock in Java

satya - 6/14/2017, 9:44:01 AM

Here is an SOF discussion on ReadWriteLock and its boundaries

Here is an SOF discussion on ReadWriteLock and its boundaries

satya - 6/14/2017, 9:45:49 AM

Alternatives to ReadWriteLock in Java

Alternatives to ReadWriteLock in Java

Search for: Alternatives to ReadWriteLock in Java

satya - 6/14/2017, 9:46:51 AM

Read-Copy-Write concurrency pattern in Java

Read-Copy-Write concurrency pattern in Java

Search for: Read-Copy-Write concurrency pattern in Java

satya - 6/14/2017, 9:49:19 AM

On ConcurrentHashMap in Java

On ConcurrentHashMap in Java

Search for: On ConcurrentHashMap in Java

satya - 6/14/2017, 9:49:40 AM

Locking alternatives in Java 8

Locking alternatives in Java 8

satya - 6/14/2017, 9:50:07 AM

Collections.synchronizedMap in Java

Collections.synchronizedMap in Java

Search for: Collections.synchronizedMap in Java

satya - 6/14/2017, 9:54:14 AM

Whats up with StampedLock in Java

Whats up with StampedLock in Java

Search for: Whats up with StampedLock in Java

satya - 6/14/2017, 10:14:50 AM

CopyOnWriteArrayList, ConcurrentHashMap in Java

CopyOnWriteArrayList, ConcurrentHashMap in Java

Search for: CopyOnWriteArrayList, ConcurrentHashMap in Java

satya - 6/14/2017, 11:14:19 AM

How to use ConcurrentModificationException in Java

How to use ConcurrentModificationException in Java

Search for: How to use ConcurrentModificationException in Java

satya - 6/14/2017, 11:18:01 AM

Java and Guava

Java and Guava

Search for: Java and Guava

satya - 6/14/2017, 11:18:08 AM

Guava

Guava

satya - 6/14/2017, 11:32:11 AM

So question for my need, now crystalises as

which of these?


ConcurrentHashMap
CopyOnWriteArrayList
ReadWriteLock
StampedLock

for my need

satya - 6/14/2017, 11:37:05 AM

Do CopyOnWriteArrayList or ConcurrentHashMap support a post-write operation?

Do CopyOnWriteArrayList or ConcurrentHashMap support a post-write operation?

Search for: Do CopyOnWriteArrayList or ConcurrentHashMap support a post-write operation?

wonder if I want to persist that map do I have a callback to do it. Or can I do it external to the core atomic write operation?

satya - 6/14/2017, 6:12:54 PM

ConcurrentHashMap Java API

ConcurrentHashMap Java API

Search for: ConcurrentHashMap Java API

satya - 6/14/2017, 6:22:15 PM

Key aspects of ConcurrentHashMap

Equivalent to a Hashtable

Retrievals (generally) do not block

Retrievals can overlap with updates (including put and remove)

Retrievals reflect the results of the most recently completed update operations holding upon their onset.

For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries.

Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration.

A value of one for concurrencyLevel (default is 16) is appropriate when it is known that only one thread will modify and all others will only read. Choose a good number

satya - 6/15/2017, 5:36:59 PM

On the other hand

If the frequency of reads and writes only a few, a simple set of synchronized methods may do the trick. So what is few?

100
1000
100000
1000000

I don't know the answer but I expect that limit to be in tens of thousands a day.

satya - 6/18/2017, 4:19:56 PM

Books on java concurrency

Books on java concurrency

Search for: Books on java concurrency

satya - 6/18/2017, 4:25:11 PM

Topics in Java Concurrency Java 8

Topics in Java Concurrency Java 8

Search for: Topics in Java Concurrency Java 8

satya - 6/18/2017, 4:30:27 PM

A good start: Java concurrency basics

A good start: Java concurrency basics