各種鎖的理解 公平鎖、非公平鎖 公平鎖:先到先得(不可插隊) 非公平鎖:達者為先(可插隊) >預設 public ReentrantLock() { //預設非公平鎖 sync = new NonfairSync(); } //重載的構造方法,通過fair控制是否公平 public Reentran ...
各種鎖的理解
公平鎖、非公平鎖
公平鎖:先到先得(不可插隊)
非公平鎖:達者為先(可插隊)---------->預設
public ReentrantLock() {
//預設非公平鎖
sync = new NonfairSync();
}
//重載的構造方法,通過fair控制是否公平
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
可重入鎖(遞歸鎖)
所有的鎖都是可重入鎖
Synchronized版
package org.example.lock;
public class Demo01 {
public static void main(String[] args) {
phone1 p1 = new phone1();
new Thread(()->{
p1.ems();
},"A").start();
new Thread(()->{
p1.ems();
},"B").start();
}
}
class phone1{
public synchronized void ems(){
System.out.println(Thread.currentThread().getName()+"---------->ems");
call();
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName()+"---------->call");
}
}
ems方法中包含了call方法,所以當我們調用ems方法獲取到鎖時,也把call方法的synchronized鎖獲取到了。
錯誤理論
- 當線程A運行ems方法後運行call方法時ems鎖釋放,線程B可以獲取到ems方法
正確理論
- 當線程A運行ems方法後運行call方法時ems方法的鎖還未釋放時就拿到了call方法中的鎖,當call方法的鎖釋放後ems方法的鎖才會釋放。線程B此時就可以運行ems方法了
Lock版
package org.example.lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Demo02 {
public static void main(String[] args) {
phone2 p2 = new phone2();
new Thread(()->{
p2.ems();
},"A").start();
new Thread(()->{
p2.ems();
},"B").start();
}
}
class phone2{
Lock lock = new ReentrantLock();
public void ems(){
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+"---------->ems");
call();
} catch (Exception e) {
e.printStackTrace();
} finally {
//等待call方法鎖解鎖後再解鎖
lock.unlock();
}
}
public void call(){
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+"---------->call");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
自旋鎖
spinlock(不斷嘗試直至成功)
已經見過了,就是unsafe中的自增getAndAddInt方法中的do-while迴圈就是一把自旋鎖
自己寫一把鎖
package org.example.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
public class SpinLockDemo {
//int 0
//Thread null
public static AtomicReference<Thread> atomic = new AtomicReference<>();
public static void lock(){
Thread thread = Thread.currentThread();
System.out.println("===============>"+thread.getName()+"===========>lock");
//自旋鎖,若線程等於null,則compareAndSet為true,加!就為false,就會一直迴圈
while (!atomic.compareAndSet(null,thread)){
}
}
public static void unlock(){
Thread thread = Thread.currentThread();
System.out.println("===============>"+thread.getName()+"===========>unlock");
//自旋鎖
atomic.compareAndSet(thread,null);
}
public static void main(String[] args) throws InterruptedException {
new Thread(()->{
try {
lock();
TimeUnit.SECONDS.sleep(10);
} catch (Exception e) {
e.printStackTrace();
} finally {
unlock();
}
},"A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
try {
lock();
TimeUnit.SECONDS.sleep(2);
} catch (Exception e) {
e.printStackTrace();
} finally {
unlock();
}
},"B").start();
}
}
死鎖
死鎖是什麼
死鎖測試
package org.example.lock;
import java.util.concurrent.TimeUnit;
public class DeadLockDemo {
public static void main(String[] args) {
String a = "A";
String b = "B";
new Thread(()->{new MyThread(a, b).run();},"A").start();
new Thread(()->{new MyThread(b, a).run();},"B").start();
}
}
class MyThread implements Runnable{
private String lockA;
private String lockB;
public MyThread(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
synchronized (lockA){
System.out.println(Thread.currentThread().getName()+"lock:"+lockA+"=>get"+lockB);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (lockB){
System.out.println(Thread.currentThread().getName()+"lock:"+lockB+"=>get"+lockA);
}
}
}
}
程式突然卡住死鎖如何排查?
1、使用jps-l
定位進程號
查看當前java活著的進程
2、使用jstack 進程號
查看死鎖問題
查找到一個死鎖問題!
面試或者工作中排查問題:
1、查看異常
2、查看日誌
3、查看堆棧信息