current包下ReentrantReadWriteLock的使用

ReentrantReadWriteLock中能夠生產讀鎖和寫鎖。java

讀鎖和讀鎖不互斥,寫鎖和任何讀鎖或者寫鎖都互斥。code

demo:讀鎖和讀鎖不互斥get

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讀鎖和寫鎖互斥:it

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();
}

寫鎖和寫鎖互斥:io

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();
    }
    
}
相關文章
相關標籤/搜索