目录
Lock和synchronized
为啥需要Lock
主要方法
Lock();
tryLock(long time,TimeUnit unit);超时就放弃
lockInterruptibly(); 中断
Java锁分类:
乐观锁和悲观锁:
乐观锁:
劣势:
使用场景:
劣势:
使用场景:
可重入锁:
ReentrantLock的其它方法
公平锁和非公平锁
共享锁和排它锁:
读写锁的作用:
读写锁的规则:
读锁和写锁的交互方式:
自旋锁和阻塞锁
自旋缺点:
原理:
使用场景:
最普通的获取锁,最佳实践是finally中释放锁,保证发生异常的时候锁一定被释放
/**
* 描述:Lock不会像syn一样,异常的时候自动释放锁
* 所以最佳实践是finally中释放锁,保证发生异常的时候锁一定被释放
*/
private static Lock lock = new ReentrantLock();
public static void main(String[] args) {
lock.lock();
try {
//获取本锁保护的资源
System.out.println(Thread.currentThread().getName() + "开始执行任务");
} finally {
lock.unlock();
}
}
用来获取锁,如果当前锁没有被其它线程占用,则获取成功,则返回true,否则返回false,代表获取锁失败
/**
* 描述:用TryLock避免死锁
*/
static class TryLockDeadlock implements Runnable {
int flag = 1;
static Lock lock1 = new ReentrantLock();
static Lock lock2 = new ReentrantLock();
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (flag == 1) {
try {
if (lock1.tryLock(800, TimeUnit.MILLISECONDS)) {
try {
System.out.println("线程1获取到了锁1");
Thread.sleep(new Random().nextInt(1000));
if (lock2.tryLock(800,TimeUnit.MILLISECONDS)){
try {
System.out.println("线程1获取到了锁2");
System.out.println("线程1成功获取到了2把锁");
break;
}finally {
lock2.unlock();
}
}else{
System.out.println("线程1获取锁2失败,已重试");
}
} finally {
lock1.unlock();
Thread.sleep(new Random().nextInt(1000));
}
} else {
System.out.println("线程1获取锁1失败,已重试");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (flag == 0) {
try {
if (lock2.tryLock(3000, TimeUnit.MILLISECONDS)) {
try {
System.out.println("线程2获取到了锁2");
Thread.sleep(new Random().nextInt(1000));
if (lock1.tryLock(800,TimeUnit.MILLISECONDS)){
try {
System.out.println("线程2获取到了锁1");
System.out.println("线程2成功获取到了2把锁");
break;
}finally {
lock1.unlock();
}
}else{
System.out.println("线程2获取锁1失败,已重试");
}
} finally {
lock2.unlock();
Thread.sleep(new Random().nextInt(1000));
}
} else {
System.out.println("线程2获取锁2失败,已经重试");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
TryLockDeadlock r1 = new TryLockDeadlock();
TryLockDeadlock r2 = new TryLockDeadlock();
r1.flag = 1;
r2.flag = 0;
new Thread(r1).start();
new Thread(r2).start();
}
}
执行结果:
线程1获取到了锁1
线程2获取到了锁2
线程1获取锁2失败,已重试
线程2获取到了锁1
线程2成功获取到了2把锁
线程1获取到了锁1
线程1获取到了锁2
线程1成功获取到了2把锁
相当于tryLock(long time,TimeUnit unit) 把超时时间设置为无限,在等待锁的过程中,线程可以被中断
/**
* 描述:获取锁的过程中,中断了
*/
static class LockInterruptibly implements Runnable {
private Lock lock = new ReentrantLock();
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "尝试获取锁");
try {
lock.lockInterruptibly();
try {
System.out.println(Thread.currentThread().getName() + "获取到了锁");
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "睡眠中被中断了");
} finally {
lock.unlock();
System.out.println(Thread.currentThread().getName() + "释放了锁");
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "等锁期间被中断了");
}
}
public static void main(String[] args) {
LockInterruptibly lockInterruptibly = new LockInterruptibly();
Thread thread0 = new Thread(lockInterruptibly);
Thread thread1 = new Thread(lockInterruptibly);
thread0.start();
thread1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread0.interrupt();
}
}
执行结果:
Thread-0尝试获取锁
Thread-1尝试获取锁
Thread-0获取到了锁
Thread-0睡眠中被中断了
Thread-0释放了锁
Thread-1获取到了锁
Thread-1释放了锁
可能造成ABA问题,就是不知道是不是修改过
适合并发写入少的情况,大部分是读取的场景,不加锁的能让读取的性能大幅提高
悲观锁:
比较悲观,认为如果我不锁住这个资源,别人就会来争抢,就会造成数据结果错误,所以它会锁住操作对象,Java中悲观锁的实现就是syn和Lock相关类
适合并发写入多的情况,适用于临界区持锁时间比较长的情况:
// 递归调用演示可重入锁
static class RecursionDemo{
public static ReentrantLock lock = new ReentrantLock();
private static void accessResource(){
lock.lock();
try {
System.out.println("已经对资源处理了");
if (lock.getHoldCount() < 5){
System.out.println("已经处理了"+lock.getHoldCount()+"次");
accessResource();
}
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
new RecursionDemo().accessResource();
}
}
执行结果:
已经对资源处理了
已经处理了1次
已经对资源处理了
已经处理了2次
已经对资源处理了
已经处理了3次
已经对资源处理了
已经处理了4次
已经对资源处理了
/**
* 描述:演示公平锁和非公平锁
*/
class FairLock{
public static void main(String[] args) {
PrintQueue printQueue = new PrintQueue();
Thread[] thread = new Thread[10];
for (int i = 0; i < 10; i++) {
thread[i] = new Thread(new Job(printQueue));
}
for (int i = 0; i < 5; i++) {
thread[i].start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Job implements Runnable{
PrintQueue printQueue;
public Job(PrintQueue printQueue) {
this.printQueue = printQueue;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"开始打印");
printQueue.printJob(new Object());
System.out.println(Thread.currentThread().getName()+"打印完成");
}
}
class PrintQueue{
// true 公平,false是非公平
private Lock queueLock = new ReentrantLock(true);
public void printJob(Object document){
queueLock.lock();
try {
int duration = new Random().nextInt(10)+1;
System.out.println(Thread.currentThread().getName()+"正在打印,需要"+duration+"秒");
Thread.sleep(duration * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
queueLock.unlock();
}
queueLock.lock();
try {
int duration = new Random().nextInt(10)+1;
System.out.println(Thread.currentThread().getName()+"正在打印,需要"+duration+"秒");
Thread.sleep(duration * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
queueLock.unlock();
}
}
}
执行结果:
Thread-0开始打印
Thread-0正在打印,需要10秒
Thread-1开始打印
Thread-2开始打印
Thread-3开始打印
Thread-4开始打印
Thread-1正在打印,需要2秒
Thread-2正在打印,需要2秒
Thread-3正在打印,需要2秒
Thread-4正在打印,需要4秒
Thread-0正在打印,需要2秒
Thread-0打印完成
Thread-1正在打印,需要7秒
Thread-1打印完成
Thread-2正在打印,需要8秒
Thread-2打印完成
Thread-3正在打印,需要3秒
Thread-3打印完成
Thread-4正在打印,需要8秒
Thread-4打印完成
true改为false演示非公平锁:
Lock queueLock = new ReentrantLock(false);
执行结果:
Thread-0正在打印,需要7秒
Thread-1开始打印
Thread-2开始打印
Thread-3开始打印
Thread-4开始打印
Thread-0正在打印,需要9秒
Thread-0打印完成
Thread-1正在打印,需要3秒
Thread-1正在打印,需要2秒
Thread-1打印完成
Thread-2正在打印,需要4秒
Thread-2正在打印,需要7秒
Thread-2打印完成
Thread-3正在打印,需要10秒
Thread-3正在打印,需要2秒
Thread-3打印完成
Thread-4正在打印,需要7秒
Thread-4正在打印,需要8秒
Thread-4打印完成
/**
* 描述:演示可以多个一起读,只能一个写
*/
class CinemaReadWrite{
private static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
private static ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
private static ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();
private static void read(){
readLock.lock();
try {
System.out.println(Thread.currentThread().getName() + "得到了读锁,正在读取");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + "释放了读锁");
readLock.unlock();
}
}
private static void write(){
writeLock.lock();
try {
System.out.println(Thread.currentThread().getName() + "得到了写锁,正在写入");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + "释放了写锁");
writeLock.unlock();
}
}
public static void main(String[] args) {
new Thread(()-> read(),"Thrad1").start();
new Thread(()-> read(),"Thrad2").start();
new Thread(()-> write(),"Thrad3").start();
new Thread(()-> write(),"Thrad4").start();
}
}
执行结果:
Thrad1得到了读锁,正在读取
Thrad2得到了读锁,正在读取
Thrad2释放了读锁
Thrad1释放了读锁
Thrad3得到了写锁,正在写入
Thrad3释放了写锁
Thrad4得到了写锁,正在写入
Thrad4释放了写锁
读锁插队策略:
/**
* 描述:自旋锁演示
*/
class SpinLock{
private AtomicReference sign = new AtomicReference<>();
public void lock(){
Thread currentThread = Thread.currentThread();
while (!sign.compareAndSet(null,currentThread)){
System.out.println("自旋获取失败,再次尝试");
}
}
public void unLock(){
Thread currentThread = Thread.currentThread();
sign.compareAndSet(currentThread,null);
}
public static void main(String[] args) {
SpinLock spinLock = new SpinLock();
Runnable runnable = new Runnable(){
@Override
public void run(){
System.out.println(Thread.currentThread().getName()+"开始尝试自旋锁");
spinLock.lock();
System.out.println(Thread.currentThread().getName()+"获取到了自旋锁");
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
spinLock.unLock();
System.out.println(Thread.currentThread().getName()+"释放了自旋锁");
}
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
}
}
执行结果:
Thread-0开始尝试自旋锁
Thread-0获取到了自旋锁
Thread-1开始尝试自旋锁
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
自旋获取失败,再次尝试
Thread-0释放了自旋锁
Thread-1获取到了自旋锁
Thread-1释放了自旋锁