Immutability and Thread Safety in Java Part-4  

by ne on 2021-09-29 under Java

There are 4 sections of this Article:
Immutability and Thread Safety Part1- Covers Immutability
Immutability and Thread Safety Part2- Covers Thread Safety
Immutability and Thread Safety Part3- More on Thread Safety and Concurrency
[This one] Immutability and Thread Safety Part4 - Covers How the above 2 are related, and some further concepts

 

If you want to know the basics of a Thread , refer : Basics of Threads in Java

We covered the basics of immutability and how to create an immutable class in part1 and about thread safety ways  in part2 and part3  of this article series.

Now, how immutability is related to thread safety.Thread safety is breached when multiple threads are able to modify the shared resources(or java objects) in an multi-threaded environment, but what if the objects being shared across the threads, are actually non-modifiable.

Yes, so if an ideal immutable object (unmodifiable after construction), is shared across different threads, we don't need to worry about threading issues anymore.

So, an Ideal Immutable Object  =>  Read-Only  + Thread safe.

We saw the use of basic Reentrant locks in previous article ,further depending on our use case, we can be more specific and optimize our locking mechanism further by making use of readLocks and writeLocks.

 



import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by navsoni on 8/10/17.
 */
public class UseLock {

    ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();


    public void readData() {
        Lock readLock = rwLock.readLock();
        try {
            if (readLock.tryLock(1000, TimeUnit.MILLISECONDS)) {
                /**
                 * do the read only work here
                 */
            }
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } finally {
            readLock.unlock();
        }
    }

    public void writeData() {
        Lock writeLock = rwLock.writeLock();
        try {
            if (writeLock.tryLock(1000, TimeUnit.MILLISECONDS)) {
                /**
                 * do the write/read work here
                 */
            }
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } finally {
            writeLock.unlock();
        }
    }
}

 

This gives more clarity to the code, and is gives performance boost. Because:

  1. Multiple Threads can acquire readLock at the same time
  2. If one thread is writing (has writeLock), none of the other threads can acquire any lock
  3. If any thread (or threads) are reading (have readLock), no thread can acquire write lock.

 

Thanks

There are 4 sections of this Article:
Immutability and Thread Safety Part1- Covers Immutability
Immutability and Thread Safety Part2- Covers Thread Safety
Immutability and Thread Safety Part3- More on Thread Safety and Concurrency
[This one] ​​​​​​​​​​​​​​Immutability and Thread Safety Part4 - Covers How the above 2 are related, and some further concepts