Home > Java > javaTutorial > body text

What are the usage and advantages of Java read-write lock?

WBOY
Release: 2023-04-20 15:25:08
forward
863 people have browsed it
Foreword:

Readers-Writer Lock (Readers-Writer Lock), as the name suggests, is a lock divided into two parts: a read lock and a write lock. The read lock allows multiple Threads obtain it at the same time, because the read operation itself is thread-safe, and the write lock is a mutual exclusion lock. Multiple threads are not allowed to obtain the write lock at the same time, and the write operation and the read operation are also mutually exclusive. In summary, the characteristics of read-write locks are: Read-read is not mutually exclusive, read-write is mutually exclusive, and write-write is mutually exclusive.

1. Use of read-write lock

In Java language, read-write lock is implemented using the ReentrantReadWriteLock class, where:

  • ReentrantReadWriteLock.ReadLock represents a read lock, which provides the lock method for locking and the unlock method for unlocking.

  • ReentrantReadWriteLock.WriteLock represents a write lock, which provides the lock method for locking and the unlock method for unlocking.

Its basic use is as shown in the following code:

// 创建读写锁
final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
// 获得读锁
final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
// 获得写锁
final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
// 读锁使用
readLock.lock();
try {
    // 业务代码...
} finally {
    readLock.unlock();
}
// 写锁使用
writeLock.lock();
try {
    // 业务代码...
} finally {
    writeLock.unlock();
}
Copy after login

1.1 Reading is not mutually exclusive

Multiple Two threads can obtain the read lock at the same time, which is called read and read non-mutual exclusion, as shown in the following code:

// 创建读写锁
final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
// 创建读锁
final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
Thread t1 = new Thread(() -> {
    readLock.lock();
    try {
        System.out.println("[t1]得到读锁.");
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        System.out.println("[t1]释放读锁.");
        readLock.unlock();
    }
});
t1.start();
Thread t2 = new Thread(() -> {
    readLock.lock();
    try {
        System.out.println("[t2]得到读锁.");
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        System.out.println("[t2]释放读锁.");
        readLock.unlock();
    }
});
t2.start();
Copy after login

The execution results of the above program are as follows:

What are the usage and advantages of Java read-write lock?

1.2 Read and write mutual exclusion

Read lock and write lock are mutually exclusive when used at the same time (that is, they cannot be obtained at the same time). This is called read and write mutual exclusion. Exclusion, as shown in the following code:

// 创建读写锁
final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
// 创建读锁
final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
// 创建写锁
final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
// 使用读锁
Thread t1 = new Thread(() -> {
    readLock.lock();
    try {
        System.out.println("[t1]得到读锁.");
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        System.out.println("[t1]释放读锁.");
        readLock.unlock();
    }
});
t1.start();
// 使用写锁
Thread t2 = new Thread(() -> {
    writeLock.lock();
    try {
        System.out.println("[t2]得到写锁.");
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        System.out.println("[t2]释放写锁.");
        writeLock.unlock();
    }
});
t2.start();
Copy after login

The above program execution results are as follows:

What are the usage and advantages of Java read-write lock?

1.3 Write-write mutual exclusion

Multiple threads using write locks at the same time are also mutually exclusive. This is called write-write mutual exclusion, as shown in the following code:

// 创建读写锁
final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
// 创建写锁
final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
Thread t1 = new Thread(() -> {
    writeLock.lock();
    try {
        System.out.println("[t1]得到写锁.");
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        System.out.println("[t1]释放写锁.");
        writeLock.unlock();
    }
});
t1.start();

Thread t2 = new Thread(() -> {
    writeLock.lock();
    try {
        System.out.println("[t2]得到写锁.");
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        System.out.println("[t2]释放写锁.");
        writeLock.unlock();
    }
});
t2.start();
Copy after login

The above program is executed The results are as follows:

What are the usage and advantages of Java read-write lock?

2. Advantage analysis

  • improves program execution performance:more Read-write locks can be executed simultaneously. Compared with ordinary locks that must be queued for execution under any circumstances, read-write locks improve the execution performance of the program.

  • Avoid reading temporary data: Read locks and write locks are mutually exclusive and queued for execution. This ensures that the read operation will not be half read and written. temporary data.

3. Applicable Scenarios

Read-write locks are suitable for business scenarios with more reading and less writing. At this time, read-write locks have the greatest advantage.

The above is the detailed content of What are the usage and advantages of Java read-write lock?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template