java虚拟机分为隐藏锁,显示锁
其中隐藏说只可以出力公平锁, 显示锁可以出力公平锁和非公平锁
/**
*显示锁
* @author Administrator
*/
public class DisplayLock {
private final Lock lock = new ReentrantLock();
public void getId() {
lock.lock();
System.out.println(UUID.randomUUID());
lock.unlock();
}
}
/**
*隐藏锁(内部锁)
* @author Administrator
*/
public class SynchronizedLock {
public synchronized void getId() {
System.out.println(UUID.randomUUID().toString());
}
public void getId2() {
//this==SynchronizedLock.Class
synchronized (this) {
System.out.println(UUID.randomUUID().toString());
}
}
//上面两方法相等
}
SynchronizedLock synchronizedLock = new SynchronizedLock();
synchronizedLock.getId();
System.out.println("\n");
DisplayLock displayLock=new DisplayLock();
displayLock.getId();
显示锁可以细分为很多种,见文档
关于锁的一些资料
https://www.cnblogs.com/zhengbin/p/5654805.html
https://www.cnblogs.com/hapjin/p/5492880.html
https://www.cnblogs.com/dolphin0520/p/3920373.html