java两种方式实现死锁

来源:互联网 发布:汇编语言用什么软件 编辑:程序博客网 时间:2024/06/07 21:13

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

public class LockTest implements Runnable {    private Object o1 = new Object();      private Object o2 = new Object();      private boolean flag=true;  public void run() {          if (flag) {               flag=false;             synchronized (o1) {                  System.out.println(Thread.currentThread().getName() + " have o1");                  try {                      Thread.sleep(100);                  } catch (InterruptedException e) {                      e.printStackTrace();                  }                  synchronized (o2) {                      System.out.println(Thread.currentThread().getName() + " have o2");                  }              }          } else {              flag=true;             synchronized (o2) {                  System.out.println(Thread.currentThread().getName() + " have o2");                  try {                      Thread.sleep(100);                  } catch (InterruptedException e) {                      e.printStackTrace();                  }                  synchronized (o1) {                      System.out.println(Thread.currentThread().getName() + " have o1");                  }              }          }      }   public static void main(String[] args) {     LockTest b1= new LockTest();     Thread thread1=new Thread(b1);   Thread thread2=new Thread(b1);   thread1.start();   thread2.start();    }}

(二)对于第四种情况

public class LockTest implements Runnable {    private Object o1 = new Object();      private Object o2 = new Object();      private Object o3 = new Object();      private int flag=1;  public void run() {          if (flag==1) {               flag=2;             synchronized (o1) {                  System.out.println(Thread.currentThread().getName() + " have o1");                  try {                      Thread.sleep(100);                  } catch (InterruptedException e) {                      e.printStackTrace();                  }                  synchronized (o2) {                      System.out.println(Thread.currentThread().getName() + " have o2");                  }              }          } else if(flag==2) {              flag=3;             synchronized (o2) {                  System.out.println(Thread.currentThread().getName() + " have o2");                  try {                      Thread.sleep(100);                  } catch (InterruptedException e) {                      e.printStackTrace();                  }                  synchronized (o3) {                      System.out.println(Thread.currentThread().getName() + " have o3");                  }              }          }          else  {              flag=1;             synchronized (o3) {                  System.out.println(Thread.currentThread().getName() + " have o3");                  try {                      Thread.sleep(100);                  } catch (InterruptedException e) {                      e.printStackTrace();                  }                  synchronized (o1) {                      System.out.println(Thread.currentThread().getName() + " have o1");                  }              }          }      }   public static void main(String[] args) {     LockTest b1= new LockTest();     Thread thread1=new Thread(b1);   Thread thread2=new Thread(b1);   Thread thread3=new Thread(b1);   thread1.start();   thread2.start();   thread3.start();    }}

例子不难看懂,面试的时候有可能要求当场就要写。

原创粉丝点击