All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Util.ReaderWriterLock Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.Util;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Basit Anwer
 */
public class ReaderWriterLock {
    //Basit: setting ReentrantReadWRiteLock to fair usage(true) so it may behave like .NET ReaderWriterLock
    //This will allow to operate the locks in the followin manner
    //A thread that tries to acquire a fair read lock (non-reentrantly) will block if either the write lock is held, or there is a waiting writer
    //thread. The thread will not acquire the read lock until after the oldest currently waiting writer thread has acquired and released the write lock. Of course, if a waiting writer abandons its wait, leaving one or more reader threads as the longest waiters in the queue with the write lock free, then those readers will be assigned the read lock.
    //A thread that tries to acquire a fair write lock (non-reentrantly) will block unless both the read lock and write lock are free
    //Internally it initiates a ReadLock and WriteLock

    //Advised to be used by *Mansoor*
    ReentrantReadWriteLock _syncObj = new ReentrantReadWriteLock(true);

    /**
     * If the current thread holds the writer lock
     *
     * @return true if the current thread holds the writer lock; otherwise, false.
     */
    public boolean IsWriterLockHeld() {
        return _syncObj.isWriteLockedByCurrentThread();
    }

    /**
     * AcquireReaderLock supports recursive reader-lock requests. That is, a thread can call AcquireReaderLock multiple times, which increments the lock count each time.
     * You must call ReleaseReaderLock once for each time you call AcquireReaderLock. Alternatively, you can call ReleaseLock to reduce the lock count to zero immediately.
     * Recursive lock requests are always granted immediately, without placing the requesting thread in the reader queue. Use recursive locks with caution, to avoid blocking
     * writer-lock requests for long periods.
     * 

* Takes non-interruptible Lock */ public void AcquireReaderLock() { _syncObj.readLock().lock(); } /** * Decrements the lock count. */ public void ReleaseReaderLock() { _syncObj.readLock().unlock(); } /** * AcquireWriterLock supports recursive writer-lock requests. That is, a thread can call AcquireWriterLock multiple times, which increments the lock count each time. * You must call ReleaseWriterLock once for each time you call AcquireWriterLock. Alternatively, you can call ReleaseLock to reduce the lock count to zero immediately. */ public void AcquireWriterLock() { _syncObj.writeLock().lock(); } /** * Decrements the lock count. */ public void ReleaseWriterLock() { _syncObj.writeLock().unlock(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy