Currentパッケージ下ReentrantReadWriteLockの使用


ReentrantReadWriteLockではリードロックとライトロックを生産できます.
リードロックとリードロックは反発せず、ライトロックと任意のリードロックまたはライトロックは反発します.
demo:リードロックとリードロックは反発しません
public static void main(String[] args) {
    ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    final ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    final ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();

    for (int i = 0; i < 5; i++) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    readLock.lock();
                    System.out.println(Thread.currentThread().getName() + "   ");
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(Thread.currentThread().getName() + "    ");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    readLock.unlock();
                }
            }
        }).start();

    }

}

demoリードロックとライトロックの反発:
public static void main(String[] args) {
    ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    final ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    final ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();

    for (int i = 0; i < 4; i++) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    readLock.lock();
                    System.out.println(Thread.currentThread().getName() + "   ");
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(Thread.currentThread().getName() + "    ");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    readLock.unlock();
                }
            }
        }).start();
    }

    new Thread(new Runnable() {
        public void run() {
            try {
                writeLock.lock();
                System.out.println(Thread.currentThread().getName() + "   ");
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName() + "    ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                writeLock.unlock();
            }
        }
    }).start();
}

書き込みロックと書き込みロックの反発:
public static void main(String[] args) {
    ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    final ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    final ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();

    for (int i = 0; i < 4; i++) {
        new Thread(new Runnable() {
        public void run() {
            try {
                writeLock.lock();
                System.out.println(Thread.currentThread().getName() + "   ");
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName() + "    ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                writeLock.unlock();
            }
        }
    }).start();
    }
    
}