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

📄 deadlock.java

📁 这是清华大学编写的JAVA教材中所有题目的源代码!
💻 JAVA
字号:
class A
{/*
	synchronized void methodA(B b)
	{
		String name=Thread.currentThread().getName();
		System.out.println(name+" entered A.methodA");
		
		try
		{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){ }
		
		System.out.println(name+" trying to call B.lastB");
		b.lastB();
	}
	
	synchronized void lastA()
	{
		System.out.println(" inside A.lastA");
	}
	*/
	synchronized (this)
	{
		String name=Thread.currentThread().getName();
		System.out.println(name+" entered A.methodA");
		
		try
		{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){ }
		
		System.out.println(name+" trying to call B.lastB");
		System.out.println(" inside A.lastA");
	}
}

class B
{/*
	synchronized void methodB(A a)
	{
		String name=Thread.currentThread().getName();
		System.out.println(name+" entered B.methodB");
		
		try
		{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){ }
		
		System.out.println(name+" trying to call A.lastA");
		a.lastA();
	}
	
	synchronized void lastB()
	{
		System.out.println(" inside B.lastB");
	}
	*/
	synchronized (this)
	{
		String name=Thread.currentThread().getName();
		System.out.println(name+" entered B.methodB");
		
		try
		{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){ }
		
		System.out.println(name+" trying to call A.lastA");
		System.out.println(" inside B.lastB");
	}
}

public class DeadLock implements Runnable
{
	A a=new A();
	B b=new B();
	
	DeadLock()
	{
		Thread.currentThread().setName("MainThread");
		new Thread(this).start();
		a.methodA(b);
		System.out.println("back to main thread");
	}
	
	public void run()
	{
		Thread.currentThread().setName("RacingThread");
		b.methodB(a);
		System.out.println("back to racing thread");
	}
	
	public static void main(String args[])
	{
		new DeadLock();
	}
}

⌨️ 快捷键说明

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