Mega Code Archive

 
Categories / Java / Threads
 

Synchronizing on another object

// : c13:SyncObject.java // Synchronizing on another object // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // www.BruceEckel.com. See copyright notice in CopyRight.txt. class DualSynch {   private Object syncObject = new Object();   public synchronized void f() {     System.out.println("Inside f()");     // Doesn't release lock:     try {       Thread.sleep(500);     } catch (InterruptedException e) {       throw new RuntimeException(e);     }     System.out.println("Leaving f()");   }   public void g() {     synchronized (syncObject) {       System.out.println("Inside g()");       try {         Thread.sleep(500);       } catch (InterruptedException e) {         throw new RuntimeException(e);       }       System.out.println("Leaving g()");     }   } } public class SyncObject {   public static void main(String[] args) {     final DualSynch ds = new DualSynch();     new Thread() {       public void run() {         ds.f();       }     }.start();     ds.g();   } } ///:~