Reentrant readwrite lock synchronization on isWriteLocked

user1623182

I am currently looking at implementing a session manager java class which provides functionality to read and refresh session tokens on demand. If the session token is being refreshed (i.e. being fetched from server) then session token readers should block until the refresh is complete. Similarly, a refresh request is blocked until any ongoing reads are not complete. Since session token read requests are quite frequent as compared to session token refresh requests, i have decided to use the ReentrantReadWriteLock to achieve the synchronisation between read and refresh. Here is how it looks:

  String refreshToken() {
         try{
             if (readWriteLock.writeLock().trylock()) {
                 //fetch session from server and store on disk
             }
        } finally {
              readWriteLock.writeLock().unlock();  
        }
        return readToken();
    }

   String readToken() {
         try {
         readWriteLock.readLock().lock();
         //read token from disk
         } finally {
               readWriteLock.readLock().unlock();
         }
    return token;
    }

}

My first attempt was to use a tryLock() on the write lock so that if it is locked for writing already then, tryLock() will return false and then acquire a read lock and block until the write lock is released thereby rescheduling read-blocked threads to complete the read. This logic works well for the case where multiple threads invoke refreshSession() at the same time thereby allowing only one thread to initiate the session token refresh whereas all other threads fall through and block on the read lock.

However, the logic above would fail if a thread had just acquired a read lock (by calling readToken()) and another thread invokes refreshToken() to acquire a write lock - the tryLock() would fail in this case missing the refresh request as a result.

As an alternative, I was looking at the readWriteLock.isWriteLocked() method that checks if any thread has acquired the write lock:

String refreshToken() {
    try{
        if (!readWriteLock.isWriteLocked()) {
            readWriteLock.writeLock().lock();
            //fetch session from server and store on disk
        } finally{
              readWriteLock.writeLock().unlock();  
        }
    }
    return readToken();
}

However, i do not have much experience with this method and not entirely sure about the synchronisation repercussions it would have since i want to ensure only one thread can acquire the write lock and subsequent requests fall through to a read. Any suggestions/ pointers would be most appreciated.

John Vint

You can introduce a new ReentrantLock and tryLock there. The ReentrantLock promises only one writer while fails quickly if there exists a writer.

ReentrantLock lock = new ReentrantLock();

public String refreshToken() {
    if (lock.tryLock()) {
        readWriteLock.writeLock().lock();
        try {
            // fetch session from server and store on disk
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
    return readToken();
}

So only one thread will ever try to write. And no threads can read while writing is progressing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reentrant code and local variables

From Dev

How to make a multiple-read/single-write lock from more basic synchronization primitives?

From Dev

Synchronization: Why is it preferred to lock a private final static object instead of the class's class object?

From Dev

readonly public, readwrite private property

From Dev

What Lock is not reentrant i.e. blocks if acquired in the same thread without previously releasing?

From Dev

nested lock/free calls in shared memory synchronization using semaphores

From Dev

Shared-memory IPC synchronization (lock-free)

From Dev

Name based Reentrant Lock in Java

From Dev

Are static member functions reentrant?

From Dev

Thread synchronization- When does a thread release the lock on an object

From Dev

Is a Common Lisp bordeaux-threads lock equivalent to Java synchronization?

From Dev

Reentrant lock - Java concurrency in practice

From Dev

Mixing lock-less and lock-full thread synchronization with atomic TestAndSet in C++

From Dev

Reentrant Synchronization- Unlocking of called synchronized method

From Dev

Reentrancy and Reentrant in C?

From Dev

Problems with reentrant Flex and Bison

From Dev

Java Multihreading Synchronization with Lock

From Dev

What will be if reassign reference to lock object inside synchronization block?

From Dev

Java 8 Reentrant Lock and Condition results in IllegalMonitorStateException: current thread is not owner

From Dev

How does Java manage synchronization under the hood? By not suspending the lock holder thread, or by letting it to hold the lock, even when suspended?

From Dev

Does reentrant lock in concurrenthashmap uses it's optional “fairness” parameter?

From Dev

What Lock is not reentrant i.e. blocks if acquired in the same thread without previously releasing?

From Dev

Thread Synchronization between class A and B: Where should the lock go?

From Dev

Reentrant Synchronization- Unlocking of called synchronized method

From Dev

readwrite and readonly mutually exclusive

From Dev

Java unlock ReadWrite lock safely?

From Dev

How implementation of Re-entrant lock and Synchronization is different

From Dev

Synchronisation of Bow/Bower example in java oracle tutorial with reentrant lock

From Dev

Conditional synchronization, lock

Related Related

  1. 1

    Reentrant code and local variables

  2. 2

    How to make a multiple-read/single-write lock from more basic synchronization primitives?

  3. 3

    Synchronization: Why is it preferred to lock a private final static object instead of the class's class object?

  4. 4

    readonly public, readwrite private property

  5. 5

    What Lock is not reentrant i.e. blocks if acquired in the same thread without previously releasing?

  6. 6

    nested lock/free calls in shared memory synchronization using semaphores

  7. 7

    Shared-memory IPC synchronization (lock-free)

  8. 8

    Name based Reentrant Lock in Java

  9. 9

    Are static member functions reentrant?

  10. 10

    Thread synchronization- When does a thread release the lock on an object

  11. 11

    Is a Common Lisp bordeaux-threads lock equivalent to Java synchronization?

  12. 12

    Reentrant lock - Java concurrency in practice

  13. 13

    Mixing lock-less and lock-full thread synchronization with atomic TestAndSet in C++

  14. 14

    Reentrant Synchronization- Unlocking of called synchronized method

  15. 15

    Reentrancy and Reentrant in C?

  16. 16

    Problems with reentrant Flex and Bison

  17. 17

    Java Multihreading Synchronization with Lock

  18. 18

    What will be if reassign reference to lock object inside synchronization block?

  19. 19

    Java 8 Reentrant Lock and Condition results in IllegalMonitorStateException: current thread is not owner

  20. 20

    How does Java manage synchronization under the hood? By not suspending the lock holder thread, or by letting it to hold the lock, even when suspended?

  21. 21

    Does reentrant lock in concurrenthashmap uses it's optional “fairness” parameter?

  22. 22

    What Lock is not reentrant i.e. blocks if acquired in the same thread without previously releasing?

  23. 23

    Thread Synchronization between class A and B: Where should the lock go?

  24. 24

    Reentrant Synchronization- Unlocking of called synchronized method

  25. 25

    readwrite and readonly mutually exclusive

  26. 26

    Java unlock ReadWrite lock safely?

  27. 27

    How implementation of Re-entrant lock and Synchronization is different

  28. 28

    Synchronisation of Bow/Bower example in java oracle tutorial with reentrant lock

  29. 29

    Conditional synchronization, lock

HotTag

Archive