0

有人可以解释一下这里同步块的对象引用表达式是什么意思吗?

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();  
  }  
}  

像在这里同步块中的资源1在这里做什么?

4

3 回答 3

3

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

这个对象有各种方法来进行这种同步,例如“等待”和“通知”,用于进一步扩展同步支持。对象上的同步块内的线程可以调用“wait”来释放锁,并等待另一个线程在同一对象上调用“notify”或“notifyAll”。当该线程随后被唤醒时,它将尝试重新获取由该对象表示的锁。这对于编写条件变量/监视器很有用。

于 2014-07-09T13:20:38.990 回答
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();  
  }  
}
于 2014-07-09T13:22:47.437 回答
0

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

于 2014-07-09T13:25:42.633 回答