对象引用表达式在synchronized块中的含义

时间:2014-07-09 13:06:06

标签: java synchronized-block

有人可以解释一下对象引用表达式在这里是同步块的含义吗?

synchronized (object reference expression) {   
  //code block   
}  

public class DeadlockExample {  
  public static void main(String[] args) {  
    final String resource1 = "ratan jaiswal";  
    final String resource2 = "vimal jaiswal";  
    // t1 tries to lock resource1 then resource2  
    Thread t1 = new Thread() {  
      public void run() {  
          synchronized (resource1) {  
           System.out.println("Thread 1: locked resource 1");  

           try { Thread.sleep(100);} catch (Exception 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(100);} catch (Exception e) {}  

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


    t1.start();  
    t2.start();  
  }  
}  

喜欢这里同步块中的resource1在这里做什么?

3 个答案:

答案 0 :(得分:3)

简而言之,对象可以用作互斥锁。给定单个对象(例如resource1),在同一资源实例上的同步块内只能有一个以上的线程。其他等待的线程将等到第一个线程退出块。

此对象有多种用于此同步的方法,例如" wait"和"通知",用于进一步扩展同步支持。对象上同步块内的线程可以调用" wait"释放锁并等到另一个线程调用"通知"或" notifyAll"在同一个对象上。当该线程随后唤醒时,它将尝试重新获取此对象所表示的锁。这对于编写condition variables / monitors非常有用。

答案 1 :(得分:1)

您正在编写一个程序来模拟死锁.. 它是如何工作的

public class DeadlockExample {  
  public static void main(String[] args) {  
    final String resource1 = "ratan jaiswal";  
    final String resource2 = "vimal jaiswal";  
    // t1 tries to lock resource1 then resource2  
    Thread t1 = new Thread() {  
      public void run() {  
          synchronized (resource1) {  //try to get lock on String resource1, no toher thread can access resource1 until this synchronized block ends (provided you've indeed entered this block by acquiring the lock..)
           System.out.println("Thread 1: locked resource 1");  

           try { Thread.sleep(100);} catch (Exception e) {}  //sleep() doesn't release the lock

           synchronized (resource2) {  //try to get lock on string get lock on String 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) {  //try to get lock on String resource2
          System.out.println("Thread 2: locked resource 2");  

          try { Thread.sleep(100);} catch (Exception e) {}  //sleep() doesn't release the lock

          synchronized (resource1) {  //try to get lock on String resource1
            System.out.println("Thread 2: locked resource 1");  
          }  
        }  
      }  
    };  


    t1.start();  
    t2.start();  
  }  
}

答案 2 :(得分:0)

应该对不在变量上的对象或方法使用同步。你以错误的方式使用它。对象同步意味着,您将锁定对象。没有其他线程可以访问。处理完成后,锁定即被释放。