deadlock.java
来自「Java程序设计技巧与开发实例附书源代码。」· Java 代码 · 共 47 行
JAVA
47 行
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("\tAccount 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 + =
减小字号Ctrl + -
显示快捷键?