【转载】Java多线程编程2--同步锁定--死锁

1、死锁实例
   Java线程死锁是一个经典的多线程问题,因为不同的线程都在等待根本不可能被释放的锁,从而导致所有的任务都无法继续完成。在多线程技术中,“死锁”是必须避免的,因为这会造成线程的“假死”。

  1. public class DeathThread implements Runnable {  
  2.     public String username;  
  3.     public Object lock1 = new Object();  
  4.     public Object lock2 = new Object();  
  5.   
  6.     public void setFlag(String username) {  
  7.         this.username = username;  
  8.     }  
  9.   
  10.     public void run() {  
  11.         if (username.equals(“a”)) {  
  12.             synchronized (lock1) {  
  13.                 try {  
  14.                     System.out.println(”username = ” + username);  
  15.                     Thread.sleep(2000);  
  16.                 } catch (InterruptedException e) {  
  17.                     e.printStackTrace();  
  18.                 }  
  19.   
  20.                 synchronized (lock2) {  
  21.                     System.out.println(”按lock1->lock2代码顺序执行了”);  
  22.                 }  
  23.             }  
  24.         }  
  25.   
  26.         if (username.equals(“b”)) {  
  27.             synchronized (lock2) {  
  28.                 try {  
  29.                     System.out.println(”username = ” + username);  
  30.                     Thread.sleep(2000);  
  31.                 } catch (InterruptedException e) {  
  32.                     e.printStackTrace();  
  33.                 }  
  34.   
  35.                 synchronized (lock1) {  
  36.                     System.out.println(”按lock1->lock2代码顺序执行了”);  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41. }  
public class DeathThread implements Runnable {
    public String username;
    public Object lock1 = new Object();
    public Object lock2 = new Object();

    public void setFlag(String username) {
        this.username = username;
    }

    public void run() {
        if (username.equals("a")) {
            synchronized (lock1) {
                try {
                    System.out.println("username = " + username);
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (lock2) {
                    System.out.println("按lock1->lock2代码顺序执行了");
                }
            }
        }

        if (username.equals("b")) {
            synchronized (lock2) {
                try {
                    System.out.println("username = " + username);
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (lock1) {
                    System.out.println("按lock1->lock2代码顺序执行了");
                }
            }
        }
    }
}
  1. public class Run {  
  2.     public static void main(String[] args) throws InterruptedException {  
  3.         DeathThread deathThread = new DeathThread();  
  4.         deathThread.setFlag(”a”);  
  5.         Thread thread1 = new Thread(deathThread);  
  6.         thread1.start();  
  7.         Thread.sleep(1000);  
  8.   
  9.         deathThread.setFlag(”b”);  
  10.         Thread thread2 = new Thread(deathThread);  
  11.         thread2.start();  
  12.     }  
  13. }  
public class Run {
    public static void main(String[] args) throws InterruptedException {
        DeathThread deathThread = new DeathThread();
        deathThread.setFlag("a");
        Thread thread1 = new Thread(deathThread);
        thread1.start();
        Thread.sleep(1000);

        deathThread.setFlag("b");
        Thread thread2 = new Thread(deathThread);
        thread2.start();
    }
}
username = a
username = b
出现死锁

     死锁是程序设计的Bug.在设计程序时就要避免双方互相持有对方的锁的情况。需要说明的是,本实验使用synchronized嵌套的代码结构来实现死锁,其实不使用嵌套的synchronized代码结构也会出现死锁,与嵌套不嵌套无任何的关系,不要被代码结构所误导。

产生死锁的四个必要条件:
(1) 互斥条件:一个资源每次只能被一个进程使用。
(2) 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。
(4) 循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值