Java Concurrency Tutorial
Selected Reading
- Concurrency - Discussion
- Concurrency - Useful Resources
- Concurrency - Quick Guide
- ConcurrentNavigableMap
- Concurrency - ConcurrentMap
- Concurrency - BlockingQueue
- Concurrency - Fork-Join framework
- Concurrency - Futures and Callables
- ScheduledThreadPoolExecutor
- Concurrency - ThreadPoolExecutor
- newSingleThreadExecutor
- newScheduledThreadPool
- Concurrency - newCachedThreadPool
- Concurrency - newFixedThreadPool
- ScheduledExecutorService
- Concurrency - ExecutorService
- Concurrency - Executor
- Concurrency - AtomicReferenceArray
- Concurrency - AtomicLongArray
- Concurrency - AtomicIntegerArray
- Concurrency - AtomicReference
- Concurrency - AtomicBoolean
- Concurrency - AtomicLong
- Concurrency - AtomicInteger
- Concurrency - Condition
- Concurrency - ReadWriteLock
- Concurrency - Lock
- Concurrency - ThreadLocalRandom
- Concurrency - ThreadLocal
- Concurrency - Deadlock
- Concurrency - Synchronization
- Interthread Communication
- Concurrency - Major Operations
- Concurrency - Environment Setup
- Concurrency - Overview
- Concurrency - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Concurrency - ReadWriteLock
Java Concurrency - ReadWriteLock Interface
A java.util.concurrent.locks.ReadWriteLock interface allows multiple threads to read at a time but only one thread can write at a time.
Read Lock − If no thread has locked the ReadWriteLock for writing then multiple thread can access the read lock.
Write Lock − If no thread is reading or writing, then one thread can access the write lock.
Lock Methods
Following is the pst of important methods available in the Lock class.
Sr.No. | Method & Description |
---|---|
1 | pubpc Lock readLock() Returns the lock used for reading. |
2 | pubpc Lock writeLock() Returns the lock used for writing. |
Example
The following TestThread program demonstrates these methods of the ReadWriteLock interface. Here we ve used readlock() to acquire the read-lock and writeLock() to acquire the write-lock.import java.util.concurrent.locks.ReentrantReadWriteLock; pubpc class TestThread { private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); private static String message = "a"; pubpc static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new WriterA()); t1.setName("Writer A"); Thread t2 = new Thread(new WriterB()); t2.setName("Writer B"); Thread t3 = new Thread(new Reader()); t3.setName("Reader"); t1.start(); t2.start(); t3.start(); t1.join(); t2.join(); t3.join(); } static class Reader implements Runnable { pubpc void run() { if(lock.isWriteLocked()) { System.out.println("Write Lock Present."); } lock.readLock().lock(); try { Long duration = (long) (Math.random() * 10000); System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " seconds."); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println(Thread.currentThread().getName() +": "+ message ); lock.readLock().unlock(); } } } static class WriterA implements Runnable { pubpc void run() { lock.writeLock().lock(); try { Long duration = (long) (Math.random() * 10000); System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " seconds."); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { message = message.concat("a"); lock.writeLock().unlock(); } } } static class WriterB implements Runnable { pubpc void run() { lock.writeLock().lock(); try { Long duration = (long) (Math.random() * 10000); System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " seconds."); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { message = message.concat("b"); lock.writeLock().unlock(); } } } }
This will produce the following result.
Output
Writer A Time Taken 6 seconds. Write Lock Present. Writer B Time Taken 2 seconds. Reader Time Taken 0 seconds. Reader: aabAdvertisements