?? deadlock.java
字號:
import java.awt.*;
import java.awt.event.*;
public class DeadLock extends Frame
{
protected static final String[] NAMES = {"A", "B"};
private int accounts[] = {1000, 1000};
private TextArea info = new TextArea(5, 40);
private TextArea status = new TextArea(5, 40);
public DeadLock()
{
super("Deadly deadlock");
setLayout(new GridLayout(2, 1));
add(makePanel(info, "Accounts"));
add(makePanel(status, "threads"));
validate();
pack();
show();
DeadLockThread A = new DeadLockThread(0, this, status);
DeadLockThread B = new DeadLockThread(1, this, status);
addWindowListener(
new WindowAdapter()
{ public void windowClosing(WindowEvent we) { System.exit(0);}}
);
}
public synchronized void transfer(int from, int into, int amount)
{
info.append("\naccount A: $" + accounts[0]);
info.append("\naccount B: $" + accounts[1]);
info.append("\n=> $ " + amount + "from " + NAMES[from] + "to " + NAMES[into]);
while (accounts[from] < amount)
{
try
{ wait(); }
catch (InterruptedException ie)
{ System.err.println("Error: " + ie); }
}
accounts[from] -= amount;
accounts[into] += amount;
notify();
}
private Panel makePanel(TextArea text, String title)
{
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add("North", new Label(title));
p.add("Center", text);
return p;
}
public static void main(String args[])
{
DeadLock bank = new DeadLock();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -