ReentrantLock与ReentrantReadWriteLock代码例子
时间: 2023-08-31 07:30:37 浏览: 97
### 回答1:
以下是使用Java编写的ReentrantLock和ReentrantReadWriteLock的代码示例:
ReentrantLock示例:
```java
import java.util.concurrent.locks.ReentrantLock;
public class Counter {
private int count = 0;
private ReentrantLock lock = new ReentrantLock();
public void increment() {
lock.lock();
try {
count++;
} finally {
lock.unlock();
}
}
public void decrement() {
lock.lock();
try {
count--;
} finally {
lock.unlock();
}
}
public int getCount() {
return count;
}
}
```
ReentrantReadWriteLock示例:
```java
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Cache {
private Object data = null;
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public Object getData() {
lock.readLock().lock();
try {
return data;
} finally {
lock.readLock().unlock();
}
}
public void setData(Object data) {
lock.writeLock().lock();
try {
this.data = data;
} finally {
lock.writeLock().unlock();
}
}
}
```
在这些示例中,ReentrantLock和ReentrantReadWriteLock都被用于控制共享资源的访问。使用lock()方法获取锁,使用unlock()方法释放锁。在ReentrantReadWriteLock示例中,readLock()和writeLock()方法被用于获取读锁和写锁,以控制对共享数据的读和写访问。
### 回答2:
ReentrantLock(可重入锁)和ReentrantReadWriteLock(可重入读写锁)是Java并发包中提供的两种锁机制。它们都是可重入锁,意味着同一个线程可以多次获取同一个锁。
首先,我们来看一下ReentrantLock的代码例子。在下面的示例中,有两个线程分别尝试获取同一把锁,并对共享资源进行操作。
```java
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
private static final ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
lock.lock();
try {
// 对共享资源进行操作
System.out.println("Thread 1 is accessing the shared resource.");
} finally {
lock.unlock();
}
});
Thread thread2 = new Thread(() -> {
lock.lock();
try {
// 对共享资源进行操作
System.out.println("Thread 2 is accessing the shared resource.");
} finally {
lock.unlock();
}
});
thread1.start();
thread2.start();
}
}
```
上述代码中,两个线程分别通过lock()方法获取锁,并在finally块中使用unlock()方法释放锁。这就确保了同一时间只能有一个线程访问共享资源。
接下来,我们来看一下ReentrantReadWriteLock的代码例子。与ReentrantLock不同,ReentrantReadWriteLock可以支持多个读线程同时访问共享资源,但只允许一个写线程进行写操作。
```java
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReentrantReadWriteLockExample {
private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private static final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
private static final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
private static int sharedResource = 0;
public static void main(String[] args) {
Thread reader1 = new Thread(() -> {
readLock.lock();
try {
// 读取共享资源
System.out.println("Reader 1 is reading the value: " + sharedResource);
} finally {
readLock.unlock();
}
});
Thread reader2 = new Thread(() -> {
readLock.lock();
try {
// 读取共享资源
System.out.println("Reader 2 is reading the value: " + sharedResource);
} finally {
readLock.unlock();
}
});
Thread writer = new Thread(() -> {
writeLock.lock();
try {
// 修改共享资源
sharedResource = 100;
System.out.println("Writer is updating the value to: " + sharedResource);
} finally {
writeLock.unlock();
}
});
reader1.start();
reader2.start();
writer.start();
}
}
```
在上述代码中,我们使用了readLock()和writeLock()方法来获取读锁和写锁,然后通过lock()方法进行加锁,通过unlock()方法进行解锁。这样保证了在写操作时,不允许其他线程同时进行读或写操作。
以上是ReentrantLock和ReentrantReadWriteLock的简单代码例子,它们在多线程并发操作中提供了可靠的锁机制,确保数据的安全性和一致性。
### 回答3:
ReentrantLock和ReentrantReadWriteLock是Java.util.concurrent包中的两个线程同步工具。它们都实现了Lock接口,可以用于对共享资源进行线程安全的访问。
首先,我们来看一下ReentrantLock的代码示例:
```java
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
private ReentrantLock lock = new ReentrantLock();
public void printMessage(String message) {
lock.lock();
try {
// 访问共享资源
System.out.println(message);
} finally {
lock.unlock(); // 释放锁
}
}
}
```
在上面的示例中,我们创建了一个ReentrantLock对象lock,并将它用于保护printMessage方法中的临界区。lock.lock()用于获取锁,如果锁已被其他线程占用,则当前线程等待。然后在try块中访问共享资源,最后在finally块中使用lock.unlock()来释放锁。
接下来,我们看一下ReentrantReadWriteLock的代码示例:
```java
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReentrantReadWriteLockExample {
private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
private String data = "Shared data";
public String readData() {
rwLock.readLock().lock();
try {
// 读取共享数据
return data;
} finally {
rwLock.readLock().unlock(); // 释放读锁
}
}
public void writeData(String input) {
rwLock.writeLock().lock();
try {
// 更新共享数据
data = input;
} finally {
rwLock.writeLock().unlock(); // 释放写锁
}
}
}
```
在上述示例中,我们创建了一个ReentrantReadWriteLock对象rwLock,并将它用于保护readData和writeData方法中的临界区。rwLock.readLock()用于获取读锁,rwLock.writeLock()用于获取写锁。多个线程可以同时获取读锁,但只有一个线程可以获取写锁。除非有线程持有写锁,否则其他线程可以同时获取读锁。在读操作中,我们使用读锁保护共享数据的只读访问,而在写操作中,我们使用写锁保护共享数据的更新。
以上是ReentrantLock和ReentrantReadWriteLock的简单代码示例,它们可以帮助我们在多线程环境中实现资源的安全访问。
阅读全文