Java ReentrantReadWriteLock Example

Java ReentrantReadWriteLock Example
In this tutorial, we will see ReentrantReadWriteLock Example. ReentrantReadWriteLock supports semantics of ReentrantLock and also ReadWriteLock.
ReadWriteLock has two locks, one for read and one for write. Request for read lock from multiple threads can be acquired if there is no write request. If there is a write request, no other thread will be able to acquire read or write lock on that resource.

ReentrantReadWriteLock example:

It is an implementation of ReadWriteLock interface.

It has two kinds of locks i.e. ReaderLock and WriterLock.

Let’s understand it with the help of an example:
We will have three runnable interfaces. Reader, WriterEven and WriterOdd.
Reader: It will read number variable.
WriterEven: It will write even digit to the number.
WriterOdd: It will write odd digit to the number.
We are going to use ReentrantReadWriteLock with fairness as true.

When you run program, you will get below output:

Read 2 —> Number is 0
Read 1 —> Number is 0
Writer odd is writing
Writer Even is writing
Read 2 —> Number is 0 1 2
Read 1 —> Number is 0 1 2
Writer odd is writing
Writer Even is writing
Read 2 —> Number is 0 1 2 3 4
Read 1 —> Number is 0 1 2 3 4
Writer odd is writing
Writer Even is writing
Read 2 —> Number is 0 1 2 3 4 5 6
Read 1 —> Number is 0 1 2 3 4 5 6
Writer odd is writing
Writer Even is writing
Read 2 —> Number is 0 1 2 3 4 5 6 7 8
Read 2 —> Number is 0 1 2 3 4 5 6 7 8
Read 1 —> Number is 0 1 2 3 4 5 6 7 8
Read 2 —> Number is 0 1 2 3 4 5 6 7 8
Read 1 —> Number is 0 1 2 3 4 5 6 7 8
Read 2 —> Number is 0 1 2 3 4 5 6 7 8
Read 2 —> Number is 0 1 2 3 4 5 6 7 8
Read 1 —> Number is 0 1 2 3 4 5 6 7 8
Read 2 —> Number is 0 1 2 3 4 5 6 7 8
Read 1 —> Number is 0 1 2 3 4 5 6 7 8
Read 2 —> Number is 0 1 2 3 4 5 6 7 8
Read 1 —> Number is 0 1 2 3 4 5 6 7 8
Read 1 —> Number is 0 1 2 3 4 5 6 7 8
Read 1 —> Number is 0 1 2 3 4 5 6 7 8

As you can see, Reader threads are blocked when writer threads have acquired writeLock and also if writerEven has acquire writeLock then writerOdd won’t able to acquire writerLock until writerEven releases it or vice versa.
lock.readLock().lock() : It is used to request for readLock.
lock.readLock().unlock() : It is used to release readLock.
lock.writeLock().lock() : It is used to acquire writeLock.
lock.writeLock().unlock() : It is used to release writeLock.


import_contacts

You may also like:

Related Posts

  • Difference between Runnable and Callable in java
    16 December

    Difference between Runnable and Callable in java

    Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread. Difference between Runnable and Callable interface in java Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable. Runnable […]

  • 13 June

    Java Executor framework tutorial with example

    Table of ContentsExecutorExecutorServiceScheduledExecutorServiceExecutorsThreadPoolExecutor Java 5 has introduced new framework called Executor Framework for managing threads.We have already seen before how to create a thread. If you have noted, we need to create an object of thread class using new Thread(runnableObject), so we need to create thread object for each task.Imagine a situation where you have […]

  • Java ExecutorCompletionService
    09 June

    Java ExecutorCompletionService

    Table of ContentsWhy you may need to use ExecutorCompletionService:Java ExecutorCompletionService example: In this post, we will see about Java ExecutorCompletionService example. ExecutorCompletionService class implements CompletionService. This class returns Future object in completion order. Why you may need to use ExecutorCompletionService: Let’s understand with the help of scenario: Let’s say you have 5 tasks, you submit it to the […]

  • 05 June

    Java newSingleThreadExecutor example

    Table of ContentsSyntax:Java newSingleThreadExecutor example:Difference between newSingleThreadExecutor and newFixedThreadPool(1): In this tutorial, we will learn about Executor’s newSingleThreadExecutor factory method. In the last tutorial, I have shared an introduction to ThreadPoolExecutor. If you are not aware of concepts of ThreadPoolExecutor, you should go through that first. Executor’s newSingleThreadExecutor factory method : This method returns thread […]

  • 29 May

    Java ScheduledThreadPoolExecutor Example

    Table of ContentsJava ScheduledThreadPoolExecutor Example:ScheduledThreadPoolExecutor’s scheduleAtFixedRate example:ScheduledThreadPoolExecutor’s scheduleWithFixedDelay example: There are multiple ways to schedule a task in java. We have already Java timer to schedule a task but the problem with timers task is that you can execute one task at a time.So if the current task takes longer subsequent job will be delayed. […]

  • Java FutureTask example
    28 May

    Java FutureTask example

    Table of ContentsJava FutureTask example:Explanation: In this tutorial, we will see about Java FutureTask example. FutureTask class has been introduced in JDK 5 with Executor Framework. FutureTask class is the concrete implementation of the Future object and provides methods for start and cancel the task.It also provides method to see if the computation is done […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.