/**
 * 死锁:两个或多个线程在执行过程中,相互争夺资源而造成的一种互相等待的现象
 * 实现一个死锁
 * <p>
 * <p>
 * 查看死锁
 * 1. 在当前类的文件夹下,打开终端运行:jps -l  获得进程号
 * 2. 继续运行:jstack <进程号>  查看死锁详细信息
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        String lock1 = "qwqw";
        String lock2 = "fadf";
        ExecutorService threadPool = new ThreadPoolExecutor(3, 5, 2, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        threadPool.execute(() -> {
            synchronized (lock1) {
                System.out.println(Thread.currentThread().getName() + "\t获得lock1,需要lock2");
                synchronized (lock2) {
                }
            }
        });

        threadPool.execute(() -> {
            synchronized (lock2) {
                System.out.println(Thread.currentThread().getName() + "\t获得lock2,需要lock1");
                synchronized (lock1) {
                }
            }
        });
    }
}