?? deadlock.java
字號(hào):
/**
* Description:
* <br/>Copyright (C), 2008-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
class A
{
public synchronized void foo( B b )
{
System.out.println("當(dāng)前線程名: " +
Thread.currentThread().getName() + " 進(jìn)入了A實(shí)例的foo方法" );
try
{
Thread.sleep(200);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
System.out.println("當(dāng)前線程名: " +
Thread.currentThread().getName() + " 企圖調(diào)用B實(shí)例的last方法");
b.last();
}
public synchronized void last()
{
System.out.println("進(jìn)入了A類的last方法內(nèi)部");
}
}
class B
{
public synchronized void bar( A a )
{
System.out.println("當(dāng)前線程名: "
+ Thread.currentThread().getName() + " 進(jìn)入了B實(shí)例的bar方法" );
try
{
Thread.sleep(200);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
System.out.println("當(dāng)前線程名: "
+ Thread.currentThread().getName() + " 企圖調(diào)用A實(shí)例的last方法");
a.last();
}
public synchronized void last()
{
System.out.println("進(jìn)入了B類的last方法內(nèi)部");
}
}
public class DeadLock implements Runnable
{
A a = new A();
B b = new B();
public void init()
{
Thread.currentThread().setName("主線程");
//調(diào)用a對(duì)象的foo方法
a.foo(b);
System.out.println("進(jìn)入了主線程之后");
}
public void run()
{
Thread.currentThread().setName("副線程");
//調(diào)用b對(duì)象的bar方法
b.bar(a);
System.out.println("進(jìn)入了副線程之后");
}
public static void main(String[] args)
{
DeadLock dl = new DeadLock();
//以dl為target啟動(dòng)新線程
new Thread(dl).start();
//執(zhí)行init方法作為新線程
dl.init();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -