WEB开发网      濂界珯濂藉垎浜紒浣犵殑涓€浠藉垎浜槸鎴戜滑鐨勪竴浠藉姩鍔涳紱璇峰垎浜� ---锕�
开发学院WEB开发Jsp 一个死锁的例子 阅读

一个死锁的例子

 2008-01-05 08:58:31 来源:WEB开发网 銆�鍑忓皬瀛椾綋澧炲ぇ瀛椾綋銆�  鍏虫敞榫欐尟鍗囩殑寰崥
核心提示:public class AnotherDeadLock {public static void main(String[] args) {final Object resource1 = "resource1";final Object resource2 = "resource2&qu

一个死锁的例子

public class AnotherDeadLock {
 public static void main(String[] args) {
  final Object resource1 = "resource1";
  final Object resource2 = "resource2";
  // t1 tries to lock resource1 then resource2
  Thread t1 = new Thread() {
   public void run() {
    // Lock resource 1
    synchronized (resource1) {
     System.out.PRintln("Thread 1: locked resource 1");

     try {
      Thread.sleep(50);
     } catch (InterruptedException e) {
     }

     synchronized (resource2) {
      System.out.println("Thread 1: locked resource 2");
     }
    }
   }
  };

  // t2 tries to lock resource2 then resource1
  Thread t2 = new Thread() {
   public void run() {
    synchronized (resource2) {
     System.out.println("Thread 2: locked resource 2");

     try {
      Thread.sleep(50);
     } catch (InterruptedException e) {
     }

     synchronized (resource1) {
      System.out.println("Thread 2: locked resource 1");
     }
    }
   }
  };

  // If all goes as planned, deadlock will occur,
  // and the program will never exit.
  t1.start();
  t2.start();
 }
}


Tags:一个 死锁 例子

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接