在 Java 中,有多种锁的实现方式来实现并发控制,这些锁提供了不同的特性和适用场景。下面我将介绍几种常见的锁实现方式,并提供相关的示例代码和依赖坐标。
synchronized
是 Java 中最基本的同步机制,可以用于方法或代码块。它基于对象监视器(或称为内部锁)来实现线程同步。
步骤流程:
示例代码:
public class SynchronizedExample {
private int count = 0;
public synchronized void increment() {
count++;
}
public static void main(String[] args) {
SynchronizedExample example = new SynchronizedExample();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
example.increment();
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
example.increment();
}
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Count: " + example.count);
}
}
ReentrantLock
是 java.util.concurrent 包中提供的锁实现,它提供了比 synchronized 更多的灵活性和扩展性,例如可重入性、公平性等。
依赖坐标: Maven:
<dependency>
<groupId>org.codehaus.jsr166</groupId>
<artifactId>java.util.concurrent</artifactId>
<version>1.8.0</version>
</dependency>
Gradle:
implementation 'org.codehaus.jsr166:java.util.concurrent:1.8.0'
步骤流程:
ReentrantLock
实例。lock()
方法获取锁。unlock()
方法释放锁。示例代码:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
private int count = 0;
private Lock lock = new ReentrantLock();
public void increment() {
lock.lock();
try {
count++;
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
ReentrantLockExample example = new ReentrantLockExample();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
example.increment();
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
example.increment();
}
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Count: " + example.count);
}
}
ReadWriteLock
也是 java.util.concurrent 包中的一种锁实现,它区分了读操作和写操作,提高了并发读的效率。
依赖坐标: Maven:
<dependency>
<groupId>org.codehaus.jsr166</groupId>
<artifactId>java.util.concurrent</artifactId>
<version>1.8.0</version>
</dependency>
Gradle:
implementation 'org.codehaus.jsr166:java.util.concurrent:1.8.0'
步骤流程:
ReadWriteLock
实例。readLock()
方法获取读锁,多个线程可以同时持有读锁。writeLock()
方法获取写锁,只有一个线程可以持有写锁,且在持有写锁期间,其他线程无法获取读锁或写锁。示例代码:
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockExample {
private int value = 0;
private ReadWriteLock lock = new ReentrantReadWriteLock();
public int readValue() {
lock.readLock().lock();
try {
return value;
} finally {
lock.readLock().unlock();
}
}
public void writeValue(int newValue) {
lock.writeLock().lock();
try {
value = newValue;
} finally {
lock.writeLock().unlock();
}
}
public static void main(String[] args) {
ReadWriteLockExample example = new ReadWriteLockExample();
Thread readerThread = new Thread(() -> {
int val = example.readValue();
System.out.println("Read value: " + val);
});
Thread writerThread = new Thread(() -> {
example.writeValue(42);
System.out.println("Value written.");
});
readerThread.start();
writerThread.start();
try {
readerThread.join();
writerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
以上是几种常见的 Java 锁实现方式及其使用示例。每种锁机制都适用于不同的场景,你可以根据具体需求选择适合的锁来实现并发控制。