Java 公平锁|非公平锁|可重入锁|递归锁|自旋锁

公平锁

是指多个线程按照申请锁的顺序来获取锁类似排队打饭,先来后到

非公平锁

是指在多线程获取锁的顺序并不是按照申请锁的顺序,有可能后申请的线程比先申请的线程优先获取到锁,在高并发的情况下,有可能造成优先级反转或者饥饿现象

两者区别

公平锁

Threads acquire a fair lock in the order in which they requested it.
公平锁,就是很公平,在并发环境中,每个线程在获取锁时会先查看此锁维护的等待队列,如果为空,或者当前线程是等待队列的第一个,就占有锁,否则就会加入到等待队列中,以后会按照 FIFO 的规则从队列中取到自己

非公平锁

a nonfair lock permits barging: threads requesting a lock can jump ahead of the queue of waiting threads if the lock happens to be available when it is requested.
非公平锁比较粗鲁,上来就直接尝试占有锁,如果尝试失败,就再采用类似公平锁那种方式。

可重入锁

可重入锁(也叫做递归锁),指的是同一线程外层函数获得锁之后,内层递归函数仍然能获取该锁的代码,在同一个线程在外层方法获取锁的时候,在进入内层方法会自动获取锁。也即是说,线程可以进入任何一个它已经拥有的锁所同步着的代码块

可重入锁最大的作用是避免死锁

在 java 中,synchronized 和 ReenterLock 都是可重入锁。synchronized 是非公平锁,ReentrantLock 可以通过构造方法传布尔值来确定是公平锁还是非公平锁。

SyncDemo

class People{
	public synchronized void look() {
		System.out.println(Thread.currentThread().getName()+"\t invoked look()");
		eat();
	}
	public synchronized void eat() {
		System.out.println(Thread.currentThread().getName()+"\t invoked eat()");
	}
}

/* Java 可重入锁 SynchronizedDemo 示例 */
public class SynchronizedDemo {
	public static void main(String[] args) {
		People people = new People();
		new Thread(()->{
			people.look();
		},"t1").start();

		new Thread(()->{
			people.look();
		},"t2").start();
	}
}

ReentrantLockDemo

class People{
	ReentrantLock lock = new ReentrantLock();
	public void look() {
		lock.lock();
		System.out.println(Thread.currentThread().getName()+"\t invoked look()");
		eat();
		lock.unlock();
	}
	public void eat() {
		lock.lock();
		System.out.println(Thread.currentThread().getName()+"\t invoked eat()");
		lock.unlock();
	}
}

/* Java 可重入锁 ReentrantLockDemo 示例 */
public class ReentrantLockDemo {
	public static void main(String[] args) {
		People people = new People();
		new Thread(()->{
			people.look();
		},"t1").start();

		new Thread(()->{
			people.look();
		},"t2").start();
	}
}

自旋锁

自旋锁(spinlock)是指尝试获取锁的线程不会立即阻塞,而是采用循环的方式去尝试获取锁,这样的好处是减少线程上下文切换的消耗,缺点是循环会消耗CPU。

手撸自旋锁

自旋锁 = while循环 + CAS

import java.util.concurrent.atomic.AtomicReference;

/* 手写一个自旋锁 */
public class SpinLockDemo {
	// 原子引用 Thread
	AtomicReference<Thread> atomicReference = new AtomicReference<>();
	// 上锁
	public void spinLock(){
		Thread thread = Thread.currentThread();
		System.out.println(Thread.currentThread().getName()+"\t invoked lock");
		while(!atomicReference.compareAndSet(null, thread)) {
			// 自旋
		}
	}
	// 解锁
	public void spinUnlock(){
		Thread thread = Thread.currentThread();
		atomicReference.compareAndSet(thread, null);	//释放锁
		System.out.println(Thread.currentThread().getName()+"\t invoked unlock");
	}
	public static void main(String[] args) {
		SpinLockDemo lock = new SpinLockDemo();
		new Thread(()->{
			lock.spinLock();
			try {
				Thread.sleep(2000);//休眠5s
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			lock.spinUnlock();
		},"t1").start();

		new Thread(()->{
			lock.spinLock();
			try {
				Thread.sleep(1000);//休眠5s
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			lock.spinUnlock();
		},"t2").start();
	}
}

发布了80 篇原创文章 · 获赞 55 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/cong____cong/article/details/104445993