⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deadlockdemo.java

📁 JAVA编程思想源代码 值得一下 很难找的
💻 JAVA
字号:
package chapter13;

public class DeadLockDemo implements Runnable {

	class A {
		synchronized void a(B b) throws InterruptedException {
			System.out.println(Thread.currentThread().getName() + "进入到A类中的方法");
			Thread.sleep(1000); //给一定的时间让别的线程运行,创造死锁的条件
			b.dead();
		}

		synchronized void dead() {
			System.out.println("dead");
		}
	}

	class B {
		synchronized void b(A a) throws InterruptedException {
			System.out.println(Thread.currentThread().getName() + "进入到B类中的方法");
			Thread.sleep(1000); //给一定的时间让别的线程运行,创造死锁的条件
			a.dead();
		}

		synchronized void dead() {
			System.out.println("dead");
		}
	}

	public A a = new A();

	public B b = new B();

	public DeadLockDemo() throws InterruptedException {
		Thread.currentThread().setName("主线程");
		new Thread(this, "其他线程").start();
		a.a(b);
		System.out.println("回到主线程");
	}

	public void run() {
		try {
			b.b(a);
		} catch (InterruptedException ie) {
		}
	}

	public static void main(String[] args) throws InterruptedException {
		new DeadLockDemo();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -