目录
在多线程编程中,锁机制是确保数据一致性和线程安全的关键技术。悲观锁和乐观锁是两种常见的锁机制,它们在不同的场景下有着各自的优势和适用范围。本文将详细介绍悲观锁和乐观锁的区别,并通过 Java 代码示例展示它们的使用方法。
悲观锁与乐观锁的概念
悲观锁
悲观锁(Pessimistic Locking)假设在并发环境中会发生冲突,因此在访问共享资源时总是先加锁,确保在事务期间没有其他线程可以修改该资源。悲观锁在事务开始时就获取锁,直到事务结束时才释放锁。
乐观锁
乐观锁(Optimistic Locking)假设在并发环境中很少发生冲突,因此在访问共享资源时不立即加锁,而是等到真正需要修改资源时再检查是否有冲突。如果发现冲突,则采取补偿措施(如重试或回滚)。
悲观锁的实现
synchronized 关键字
synchronized
关键字是 Java 中最基本的悲观锁实现方式。它可以用于方法或代码块,确保同一时间只有一个线程可以执行被同步的代码。
public class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public synchronized void decrement() {
count--;
}
public synchronized int getCount() {
return count;
}
}
ReentrantLock 类
ReentrantLock
类提供了比 synchronized
更灵活的锁机制。它可以显式地获取和释放锁,并支持公平锁和非公平锁。
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Counter {
private int count = 0;
private final Lock 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() {
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
}
}
乐观锁的实现
版本号机制
版本号机制的基本思路是在数据中增加一个字段version,表示该数据的版本号,每当数据被修改,版本号加1。
当某个线程查询数据时,将该数据的版本号一起查出来;
当该线程更新数据时,判断当前版本号与之前读取的版本号是否一致,如果一致才进行操作。
需要注意的是,这里使用了版本号作为判断数据变化的标记,实际上可以根据实际情况选用其他能够标记数据版本的字段,如时间戳等。
下面以“更新玩家金币数”为例(数据库为MySQL,其他数据库同理),看看悲观锁和版本号机制是如何应对并发问题的。
考虑这样一种场景:游戏系统需要更新玩家的金币数,更新后的金币数依赖于当前状态(如金币数、等级等),因此更新前需要先查询玩家当前状态。
下面的实现方式,没有进行任何线程安全方面的保护。如果有其他线程在query和update之间更新了玩家的信息,会导致玩家金币数的不准确。
@Transactional
public void updateCoins(Integer playerId){
//根据player_id查询玩家信息
Player player = query("select coins, level from player where player_id = {0}", playerId);
//根据玩家当前信息及其他信息,计算新的金币数
Long newCoins = ……;
//更新金币数
update("update player set coins = {0} where player_id = {1}", newCoins, playerId);
}
为了避免这个问题,悲观锁通过加锁解决这个问题,代码如下所示。在查询玩家信息时,使用select …… for update进行查询;
该查询语句会为该玩家数据加上排它锁,直到事务提交或回滚时才会释放排它锁;
在此期间,如果其他线程试图更新该玩家信息或者执行select for update,会被阻塞。
@Transactional
public void updateCoins(Integer playerId){
//根据player_id查询玩家信息(加排它锁)
Player player = queryForUpdate("select coins, level from player where player_id = {0} for update", playerId);
//根据玩家当前信息及其他信息,计算新的金币数
Long newCoins = ……;
//更新金币数
update("update player set coins = {0} where player_id = {1}", newCoins, playerId);
}
版本号机制则是另一种思路,它为玩家信息增加一个字段:version。在初次查询玩家信息时,同时查询出version信息;
在执行update操作时,校验version是否发生了变化,如果version变化,则不进行更新。
@Transactional
public void updateCoins(Integer playerId){
//根据player_id查询玩家信息,包含version信息
Player player = query("select coins, level, version from player where player_id = {0}", playerId);
//根据玩家当前信息及其他信息,计算新的金币数
Long newCoins = ……;
//更新金币数,条件中增加对version的校验
int rowsUpdated =update("update player set coins = {0} where player_id = {1} and version = {2}", newCoins, playerId, player.version);
if (rowsUpdated == 0) {
throw new OptimisticLockException("Data has been modified by another transaction");
}
}
CAS 操作
原子操作(如 compareAndSet
)是另一种常见的乐观锁实现方式。Java 提供了 AtomicInteger
类,支持 CAS 操作。
import java.util.concurrent.atomic.AtomicInteger;
public class OptimisticCounter {
private AtomicInteger count = new AtomicInteger(0);
public void increment() {
int current;
int next;
do {
current = count.get();
next = current + 1;
} while (!count.compareAndSet(current, next));
}
public void decrement() {
int current;
int next;
do {
current = count.get();
next = current - 1;
} while (!count.compareAndSet(current, next));
}
public int getCount() {
return count.get();
}
}
悲观锁与乐观锁的区别
锁机制
- 悲观锁:在访问共享资源时总是先加锁,确保在事务期间没有其他线程可以修改该资源。
- 乐观锁:在访问共享资源时不立即加锁,而是等到真正需要修改资源时再检查是否有冲突。
性能
- 悲观锁:由于总是加锁,可能会导致线程阻塞,影响性能。
- 乐观锁:只有在真正需要修改资源时才检查冲突,减少了不必要的锁竞争,提高了性能。
适用场景
- 悲观锁:适用于写操作多于读操作的场景,或者对数据一致性要求极高的场景。
- 乐观锁:适用于读操作多于写操作的场景,或者冲突概率较低的场景。
示例对比
悲观锁示例
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PessimisticCounter {
private int count = 0;
private final Lock 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() {
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
}
}
乐观锁示例
import java.util.concurrent.atomic.AtomicInteger;
public class OptimisticCounter {
private AtomicInteger count = new AtomicInteger(0);
public void increment() {
int current;
int next;
do {
current = count.get();
next = current + 1;
} while (!count.compareAndSet(current, next));
}
public void decrement() {
int current;
int next;
do {
current = count.get();
next = current - 1;
} while (!count.compareAndSet(current, next));
}
public int getCount() {
return count.get();
}
}
性能测试
为了对比悲观锁和乐观锁的性能,我们可以编写一个简单的测试程序。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class LockPerformanceTest {
public static void main(String[] args) throws InterruptedException {
testPessimisticLock();
testOptimisticLock();
}
public static void testPessimisticLock() throws InterruptedException {
PessimisticCounter counter = new PessimisticCounter();
ExecutorService executor = Executors.newFixedThreadPool(10);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
executor.submit(counter::increment);
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.MINUTES);
long end = System.currentTimeMillis();
System.out.println("Pessimistic Lock Time: " + (end - start) + " ms");
System.out.println("Final Count: " + counter.getCount());
}
public static void testOptimisticLock() throws InterruptedException {
OptimisticCounter counter = new OptimisticCounter();
ExecutorService executor = Executors.newFixedThreadPool(10);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
executor.submit(counter::increment);
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.MINUTES);
long end = System.currentTimeMillis();
System.out.println("Optimistic Lock Time: " + (end - start) + " ms");
System.out.println("Final Count: " + counter.getCount());
}
}
测试结果分析
通过运行上述测试程序,我们可以观察到悲观锁和乐观锁在不同场景下的性能表现。一般来说:
- 悲观锁:由于总是加锁,可能会导致线程阻塞,特别是在写操作频繁的场景下,性能较差。
- 乐观锁:只有在真正需要修改资源时才检查冲突,减少了不必要的锁竞争,提高了性能,特别是在读操作多于写操作的场景下表现更好。
总结
悲观锁和乐观锁是两种常见的锁机制,它们在不同的场景下有着各自的优势和适用范围。悲观锁适用于写操作多于读操作的场景,或者对数据一致性要求极高的场景。乐观锁适用于读操作多于写操作的场景,或者冲突概率较低的场景。
通过本文的学习,希望读者能够更好地理解和选择合适的锁机制,以提高多线程程序的性能和可靠性。如果您有任何疑问或建议,请随时留言交流。