JAVA死锁2

来源:互联网 发布:ios 淘宝hd 编辑:程序博客网 时间:2024/05/22 12:37
public class Deadlock{      public static void main(String[  ] args) {          final Object resource1 = "resource1";          final Object resource2 = "resource2";          int a=0;          Thread t1 = new Thread( ) {                  public void run( ) {                      synchronized(resource1) {                          System.out.println("Thread 1: locked resource 1");                          try {                               Thread.sleep(50);                           }catch (InterruptedException e) {                                                          }                          synchronized(resource2) {                                  while(true){                                      System.out.println("Thread 1: locked resource 2");                                      try {                                          Thread.sleep(10000);                                      }catch (InterruptedException e) {                                                                                }                              }                          }                      }                  }              };                    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");                                                        }                      }                  }              };                    t1.start( );           t2.start( );      }  }  

原创粉丝点击