synchronized是Java语言内置的特性,用来实现对资源的同步访问以及用wait和notify来实现线程间通信。如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,并执行该代码块时,其他线程便只能一直等待,等待获取锁的线程释放锁。
存在问题:那么如果这个获取锁的线程由于要等待IO或者其他原因(比如调用sleep方法)被阻塞了,但是又没有释放锁,其他线程便只能干巴巴地等待,试想一下,这多么影响程序执行效率。因此我们需要不论程序的代码块执行的如何最终都将锁对象进行释放,方便其他线程的执行。
虽然我们可以理解同步代码块和同步方法的锁对象问题,但是我们并没有直接看到在哪里加上了锁,在哪里释放了锁,同时为了更好地释放锁,为了更清晰的表达如何加锁和释放锁,从Java5开始提供了一个新的锁对象Lock。
java.util.concurrent.locks包为锁和等待条件提供一个框架的接口和类,结构如下图所示。
(1)Lock和ReadWriteLock是两大锁根接口,Lock代表实现类是ReentrantLock(可重入锁),ReadWriteLock(读写锁)的代表实现类是ReentrantReadWriteLock。
(2)Condition 接口描述了可能会与锁有关联的条件变量。这些变量在用法上与使用 Object.wait 访问的隐式监视器类似,但提供了更强大的功能。需要特别指出的是,单个 Lock 可能与多个 Condition 对象关联。为了避免兼容性问题,Condition 方法的名称与对应的 Object 版本中的不同。
Lock接口有6个方法
// 获取锁
void lock()
// 如果当前线程未被中断,则获取锁
void lockInterruptibly()
// 返回绑定到此 Lock 实例的新 Condition 实例
Condition newCondition()
// 仅在调用时锁为空闲状态才获取该锁
boolean tryLock()
// 如果锁在给定的等待时间内空闲,并且当前线程未被中断,则获取锁
boolean tryLock(long time, TimeUnit unit)
// 释放锁
void unlock()
其中 lock与 unlock是最常用的方法,分别是获取与释放锁。
【例子1】Lock锁
package demo;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockThread {
Lock lock = new ReentrantLock();
public void lock(String name) {
// 获取锁
lock.lock();
try {
System.out.println(name + " get the lock");
// 访问此锁保护的资源
} finally {
// 释放锁
lock.unlock();
System.out.println(name + " release the lock");
}
}
public static void main(String[] args) {
LockThread lt = new LockThread();
new Thread(() -> lt.lock("A")).start();
new Thread(() -> lt.lock("B")).start();
}
}
从执行结果可以看出,A线程和B线程同时对资源加锁,A线程获取锁之后,B线程只好等待,直到A线程释放锁B线程才获得锁。
总结一下,也就是说Lock提供了比synchronized更多的功能。但是要注意以下几点:
1)synchronized是Java语言的关键字,因此是内置特性,Lock不是Java语言内置的,Lock是一个接口,通过实现类可以实现同步访问。
2)synchronized是在JVM层面上实现的,不但可以通过一些监控工具监控synchronized的锁定,而且在代码执行时出现异常,JVM会自动释放锁定,但是使用Lock则不行,lock是通过代码实现的,要保证锁定一定会被释放,就必须将unLock()放到finally{}中
3)在资源竞争不是很激烈的情况下,Synchronized的性能要优于ReetrantLock,但是在资源竞争很激烈的情况下,Synchronized的性能会下降几十倍,但是ReetrantLock的性能能维持常态。
ReadWriteLock 接口只有两个方法:
//返回用于读取操作的锁
Lock readLock()
//返回用于写入操作的锁
Lock writeLock()
ReadWriteLock 维护了一对相关的锁,一个用于只读操作,另一个用于写入操作。只要没有 writer,读取锁可以由多个 reader 线程同时保持,而写入锁是独占的。
【例子】三个线程同时对一个共享数据进行读写
package demo;
import java.util.Random;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class Queue {
//共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。
private Object data = null;
ReadWriteLock lock = new ReentrantReadWriteLock();
// 读数据
public void get() {
// 加读锁
lock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName() + " be ready to read data!");
Thread.sleep((long) (Math.random() * 1000));
System.out.println(Thread.currentThread().getName() + " have read data :" + data);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 释放读锁
lock.readLock().unlock();
}
}
// 写数据
public void put(Object data) {
// 加写锁
lock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName() + " be ready to write data!");
Thread.sleep((long) (Math.random() * 1000));
this.data = data;
System.out.println(Thread.currentThread().getName() + " have write data: " + data);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 释放写锁
lock.writeLock().unlock();
}
}
}
public class ReadWriteLockDemo {
public static void main(String[] args) {
final Queue queue = new Queue();
//一共启动6个线程,3个读线程,3个写线程
for (int i = 0; i < 3; i++) {
//启动1个读线程
new Thread() {
public void run() {
while (true) {
queue.get();
}
}
}.start();
//启动1个写线程
new Thread() {
public void run() {
while (true) {
queue.put(new Random().nextInt(10000));
}
}
}.start();
}
}
}
执行结果
Thread-0 be ready to read data!
Thread-2 be ready to read data!
Thread-4 be ready to read data!
Thread-2 have read data :null
Thread-0 have read data :null
Thread-4 have read data :null
Thread-5 be ready to write data!
Thread-5 have write data: 3872
Thread-1 be ready to write data!
Thread-1 have write data: 4744
Thread-3 be ready to write data!
Thread-3 have write data: 1371
Thread-2 be ready to read data!
Thread-0 be ready to read data!
Thread-4 be ready to read data!
Thread-2 have read data :1371
Thread-0 have read data :1371
Thread-4 have read data :1371
Thread-5 be ready to write data!
Thread-5 have write data: 2874
Thread-5 be ready to write data!
Thread-5 have write data: 389
Thread-5 be ready to write data!
Thread-5 have write data: 2512
Thread-5 be ready to write data!
Thread-5 have write data: 7455
Thread-5 be ready to write data!
Thread-5 have write data: 5141
Thread-5 be ready to write data!
Thread-5 have write data: 7709
Thread-1 be ready to write data!
Thread-1 have write data: 7670
Thread-1 be ready to write data!
Thread-1 have write data: 7144
Thread-1 be ready to write data!
Thread-1 have write data: 5673
Thread-1 be ready to write data!
Thread-1 have write data: 7810
Thread-1 be ready to write data!
Thread-1 have write data: 3937
Thread-3 be ready to write data!
Thread-3 have write data: 4259
Thread-2 be ready to read data!
Thread-0 be ready to read data!
Thread-4 be ready to read data!
Thread-0 have read data :4259
Thread-2 have read data :4259
Thread-4 have read data :4259
Thread-5 be ready to write data!
Thread-5 have write data: 9939
Thread-5 be ready to write data!
.....