一 多线程
1.1 死锁
1.1.1 死锁的案例
package com.ljf.day0814;
/**
* @ClassName: DeadLock
* @Description: TODO
* @Author: admin
* @Date: 2023/08/21 09:31:16
* @Version: V1.0
**/
public class DeadLock {
public static void main(String[] args) {
Object objA=new Object();
Object objB=new Object();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (objA){
System.out.println("我是线程objA:"+Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (objB){
System.out.println("我是线程objA:在等待获取objb"+Thread.currentThread().getName());
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (objB){
System.out.println("我是线程objB:"+Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (objA){
System.out.println("我是线程objB:在等待获取objA"+Thread.currentThread().getName());
}
}
}
}).start();
}
}
1.1.2 验证死锁
分析死锁
1.2 生产消费