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

📄 deadlock.java

📁 java编程开发技巧与实例的编译测试通过的所有例程
💻 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 + -