Java中的读写锁接口
锁是一种设备,用于命令多个线程访问分配的资源。通常,锁授予对共享资源的独占访问权限:闪存中只有一个线程可以获取锁,并且每个访问共享资源的人都需要先获取锁。但是,某些锁可能允许并行访问共享资源,如 ReadWriteLock 的读锁。
ReadWriteLock 是一个接口。 ReadWriteLock 由Java.util.concurrent.locks 包中的 ReentrantReadWriteLock 类实现。因此,要使用 ReadWriteLock,我们必须使用 ReentrantReadWriteLock。
Java.util.concurrent.locks.ReadWriteLock 是一个高级线程锁工具。它允许多个线程读取特定资源,但一次只允许一个线程写入它。
方法是,多个线程可以从共享资源中读取,而不会导致并发错误。当对共享资源的写入和读取同时发生,或者同时发生多个写入时,首先会发生并发错误。
规则:
读锁和写锁允许线程锁定 ReadWriteLock 以进行读或写。
- 读锁:如果没有线程请求写锁和写锁,那么多个线程可以锁定读锁。这意味着多个线程可以同时读取数据,只要没有线程写入数据或更新数据即可。
- 写锁:如果没有线程在写或读,那么在某一时刻只有一个线程可以锁定写锁。其他线程必须等到锁被释放。这意味着,在这一刻只有一个线程可以写入数据,其他线程必须等待。
方法: ReadWritelock 提供了两种方法:
- 锁定 readLock()
- 锁写Lock()
他们的作品与他们的名字相似。 readLock() 用于在读取时获取锁:
Lock readLock = rwLock.readLock();
在执行读操作的代码块上使用读锁:
Java
readLock.lock();
try {
// statements
}
finally {
readLock.unlock();
}
Java
writeLock.lock();
try {
statements to write the data
}
finally {
writeLock.unlock();
}
Java
// Implementation of ReadWriteLock in Java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class GFG {
private final ReadWriteLock readWriteLock
= new ReentrantReadWriteLock();
private final Lock writeLock
= readWriteLock.writeLock();
private final Lock readLock = readWriteLock.readLock();
private final List list = new ArrayList<>();
// setElement function sets
// i.e., write the element to the thread
public void setElement(O o)
{
// acquire the thread for writing
writeLock.lock();
try {
list.add(o);
System.out.println(
"Element by thread "
+ Thread.currentThread().getName()
+ " is added");
}
finally {
// To unlock the acquired write thread
writeLock.unlock();
}
}
// getElement function prints
// i.e., read the element from the thread
public O getElement(int i)
{
// acquire the thread for reading
readLock.lock();
try {
System.out.println(
"Elements by thread "
+ Thread.currentThread().getName()
+ " is printed");
return list.get(i);
}
finally {
// To unlock the acquired read thread
readLock.unlock();
}
}
public static void main(String[] args)
{
GFG gfg = new GFG<>();
gfg.setElement("Hi");
gfg.setElement("Hey");
gfg.setElement("Hello");
System.out.println("Printing the last element : "
+ gfg.getElement(2));
}
}
而且, writeLock() 用于在写入时获取锁:
Lock writeLock = rwLock.writeLock();
在执行写操作的代码块上使用写锁:
Java
writeLock.lock();
try {
statements to write the data
}
finally {
writeLock.unlock();
}
执行:
Java
// Implementation of ReadWriteLock in Java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class GFG {
private final ReadWriteLock readWriteLock
= new ReentrantReadWriteLock();
private final Lock writeLock
= readWriteLock.writeLock();
private final Lock readLock = readWriteLock.readLock();
private final List list = new ArrayList<>();
// setElement function sets
// i.e., write the element to the thread
public void setElement(O o)
{
// acquire the thread for writing
writeLock.lock();
try {
list.add(o);
System.out.println(
"Element by thread "
+ Thread.currentThread().getName()
+ " is added");
}
finally {
// To unlock the acquired write thread
writeLock.unlock();
}
}
// getElement function prints
// i.e., read the element from the thread
public O getElement(int i)
{
// acquire the thread for reading
readLock.lock();
try {
System.out.println(
"Elements by thread "
+ Thread.currentThread().getName()
+ " is printed");
return list.get(i);
}
finally {
// To unlock the acquired read thread
readLock.unlock();
}
}
public static void main(String[] args)
{
GFG gfg = new GFG<>();
gfg.setElement("Hi");
gfg.setElement("Hey");
gfg.setElement("Hello");
System.out.println("Printing the last element : "
+ gfg.getElement(2));
}
}
输出
Element by thread main is added
Element by thread main is added
Element by thread main is added
Elements by thread main is printed
Printing the last element : Hello