死锁与解决死锁

时间:2022-04-10 04:42:20

----------------------------------要是以后写数据库的产品,不是写SQL语句,那里面涉及到的锁就跟多了。


故事里有五个哲学家

这些哲学家很穷,只买得起五根筷子。他们坐成一圈,两个人的中间放一根筷子。哲学家吃饭的时候必须同时得到左手边和右手边的筷子。如果他身边的任何一位正在使用筷子,那他只有等着。

假设哲学家的编号是A、B、C、D、E,筷子编号是1、2、3、4、5,红色的是哲学家的手,哲学家和筷子围成一圈如下图所示:

 

死锁与解决死锁

实例235  门锁打不开了(死锁)_demo

 实例236 门锁终于被打开了(解决死锁) 

public class DoorOpen {
	static String[] keys = new String[]{"第1把钥匙","第2把钥匙"};
	
	static class DoorKey1 extends Thread{
		public void run(){
			synchronized(keys[0]){
				System.out.println("我拿起了" + keys[0] + ",在等着朋友用" + keys[1] + "开防盗门");  
				try{
					Thread.sleep(100);
				}catch(Exception e){
					System.out.println("线程休眠出错:" + e.getMessage());
				} 
				synchronized(keys[1]){
					System.out.println("我又拿出来"+ keys[1] + "打开了防盗门");
				}
			}
		}
	}
	
	static class DoorKey2 extends Thread { // 静态内部类  
        public void run() {  
          synchronized(keys[0]){
        	System.out.println("\n朋友拿出了" + keys[0] + ",在等待我用"  + keys[1] 
        	         + "开防盗门");
        	try{
        		Thread.sleep(100);
        	}catch (Exception e) { 
        		System.out.println("线程休眠出错:" + e.getMessage());  
        	}
        	synchronized(keys[1]){
        		System.out.println("朋友又拿出了" + keys[1] + "打开了防盗门");  
        	  }
        	}
        }
       }
        
	 static class GoWrong extends Thread { // 静态守护线程类  
		 public GoWrong(){
			 this.setDaemon(true);  // 线程设置守护  
		 }
		 public void run() {  
	            while (true) {  
	                try {  
	                    Thread.sleep(1000); // 线程休眠  
	                } catch (Exception e) { // 捕获异常  
	                    System.out.println("线程休眠出错:" + e.getMessage());  
	                }  
	                System.out.println("守护线程:程序正在运行...");  
	            }  
	        }  
	 }
	 
	 public static void main(String[] args) { // java程序主入口处  
	        DoorKey1 one = new DoorKey1(); // 实例化对象  
	        DoorKey2 two = new DoorKey2();  
	        GoWrong daemon = new GoWrong();  
	        one.start(); // 启动线程  
	        two.start();  
	        daemon.start();  
	    }  

}
死锁与解决死锁


实例237  一个死锁的例子

import java.awt.*;
import java.awt.event.*;


public class DeadLock extends Frame{
	protected static final String[] names = {"One", "Two"};
	
	private int  accounts[] = { 1000, 1000 };// 存入账号accounts
	// 创建TextArea组件
	private TextArea info = new TextArea(5,40);


	private TextArea status = new TextArea(5,40);
	
	public DeadLock(){
		super("致命的死锁!");// 调用父类Frame的带参构造方法
		this.setLayout(new GridLayout(2,1));
		add(makePanel(info, "账号"));
		add(makePanel(status, "线程"));
		validate();		//validate验证
		pack();
		show();
		// 创建DeadLockThread对象
		DeadLockThread A = new DeadLockThread(0, this, status);
		DeadLockThread B = new DeadLockThread(1, this, status);
		this.addWindowListener(new WindowAdapter() { // 添加单击事件监听   WindowAdapter 窗口适配器
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	public synchronized void transfer(int from, int into, int amount) {// 转账transfer
		info.append("\n账户  One:{1}quot; + accounts[0]);// 将给定文本追加到文本区的当前文本
		info.append("\n账户  Two:{1}quot; + accounts[1]);
		info.append("\n>={1}quot; + amount + "从" + names[from]+ "到" + names[into]);
		while (accounts[from]<amount) {
			try{
				wait();
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			accounts[from] = amount;		//账户金额(取]= $金额;
			accounts[into] = amount;		//金额帐户 [到]=金额帐户;
			notify();		//notify(); 通知();
			}
	}
	
	private Panel makePanel(TextArea ta, String title){ // 创建面板,按选择的布局方式将组件进行布局
		Panel p = new Panel();
		p.setLayout(new BorderLayout());
		p.add("North", new Label(title));
		p.add("Center", ta);
		return p;
	}
	
	public static void main(String[] args) {// 本程序的主方法
		DeadLock dl = new DeadLock();
	}
	
}

class DeadLockThread extends Thread {// 死锁线程
	private DeadLock dl;


	private int id;


	private TextArea display;		//TextArea 文字区域
	
	public DeadLockThread(int _id, DeadLock _dl, TextArea _display) {
		dl = _dl;
		id = _id;
		display = _display;
		start();
	}
	
	public void run() {
		while (true) {
			int amount = (int) (2000 * Math.random());
			display.append("\nTread" + DeadLock.names[id] + "将 {1}quot; + amount
					+ "存入" + DeadLock.names[(1 - id)]);
			try {
				sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dl.transfer(id, 1 - id, amount);
		}
	}
	
}
死锁与解决死锁