0

Can some one explain me what does object reference expression is meant over herein 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();  
  }  
}  

like over here what does resource1 in synchronized block is doing here?

user3380123
  • 703
  • 1
  • 6
  • 14
  • 2
    Did you read [the tutorial](http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html)? It explain this. If you have a more specific question, please narrow it down. – yshavit Jul 09 '14 at 13:09
  • i just wanted to know why are we passing a string in synchronized block ? – user3380123 Jul 09 '14 at 13:13
  • 1
    I doesn't matter that it's a string, it's just any reference – yshavit Jul 09 '14 at 13:14
  • So what does it do with the reference? – user3380123 Jul 09 '14 at 13:15
  • Did you read the link I mentioned above? It answers that question. – yshavit Jul 09 '14 at 13:15
  • I can't find synchronized block over there...cant u just pls explain it? – user3380123 Jul 09 '14 at 13:19
  • 2
    @user3380123 - You should not code what you don't understand.. First understand the underlying concepts then code.. – TheLostMind Jul 09 '14 at 13:27
  • Wait, now I'm curious. You can search for "synchronized" on that page and it'll show you the blocks. They're in code blocks that are visually separate from the rest of the text. How are you not finding them? Learning to read documentation is an absolutely critical part of bing a developer. – yshavit Jul 09 '14 at 13:35

3 Answers3

3

In short, objects can be used as mutual exclusion locks. Given a single object (such as resource1), no more than one thread can be inside a synchronized block over the same instance of the resource. The other waiting threads would wait until the first thread exits the block.

This object has various methods for this synchronization such as "wait" and "notify", used to further extend the synchronization support. A thread inside a synchronization block on an object may call "wait" to release the lock and wait until another thread calls "notify" or "notifyAll" on that same object. When that thread then wakes up, it will try to re-acquire the lock represented by this object. This is useful for writing condition variables / monitors.

user3820547
  • 359
  • 1
  • 5
  • Good answer, but based on original question and comments, It's not clear that the OP understands that a String _is_ an Object... or, understands the differences between variables, expressions, objects, etc. – Solomon Slow Jul 09 '14 at 13:56
1

You are writing a program to simulate a deadlock.. how it works?

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();  
  }  
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Synchronisation should be used on an object or methods not on variables. You have used it in wrong way. Synchronisation on object means, you will be having a lock over the object. No other threads can access. Once the processing is done, lock is released.

Venkatesh K
  • 133
  • 1
  • 1
  • 11