Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaWhat is Lock Striping in Java Concurrency?

What is Lock Striping in Java Concurrency?

Lock striping is where the locking happens on a few containers or stripes or buckets, implying that getting to a pail just bolts that can and not the whole information structure. That is the idea of lock striping. Having separate locks for a bit of an information structure where each lock is locking on a variable measured arrangement of autonomous items. Let’s take an example of ConcurrentHashMap.

Example:

As a matter of course ConcurrentHashMap in Java has 16 buckets and each container has its own lock system. So the strings which are getting to keys in discrete pails can get to them at the same time. If you need to imagine it, at that point following picture would give you a thought of how to lock striping for a ConcurrentHashMap will resemble.  

Here two strings need to get to keys in can 0 so one of them can enter, again two strings need to get to keys in basin 1 so one of them can enter. Same with pail n-3. Along these lines, with lock striping out of 6 strings, 3 strings can chip away at the information structure.

To discover a bucket for the new component, we are depending on the hashcode of the key. And to include more buckets, we should duplicate the current hash table and make another table with, state, and double the size.

Features of Lock Striping :

  1. It occurs only on some buckets/containers.
  2. It helps in adding two or more elements to different containers.

Role of Lock Striping in Java Concurrency :

  1. It handles the concurrent access to data while having a good performance.
  2. It helps the threads for accessing keys in separate buckets/containers.

Java Concurrency Downside :

In the case of Lock Striping in Java Concurrency applying lock and if you need a collection for exclusive access than with a single lock then it will more difficult and costly. 

RELATED ARTICLES

Most Popular

Recent Comments