Results 1 to 3 of 3
  1. #1
    Jackson86 is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Can't understand what Joshua Bloch is talking about...

    Hi, folks ! )

    I'm a newby in Java concurrency world. And I can't understand following article
    from 'Effective Java' by Joshua Bloch:

    Chapter 9

    Item 49: Avoid excessive synchronization

    "Invoking an alien method from within a synchronized region can cause failures more severe than deadlocks if the alien method is invoked while the invariants protected by the synchronized region are temporarily invalid. (This cannot happen in the broken work queue example because the queue is in a consistent state when processItem is invoked.) Such failures do not involve the creation of a new thread from within the alien method; they occur when the alien method itself calls back in to the faulty class. Because locks in the Java programming language are recursive, such calls won't deadlock as they would if they were made by another thread. The calling thread already holds the lock, so the thread will succeed when it tries to acquire the lock a second time, even though there is another conceptually unrelated operation in progress on the data protected by the lock. The consequences of such a failure can be catastrophic; in essence, the lock has failed to do its job. Recursive locks simplify the construction of multithreaded object-oriented programs, but they can turn liveness failures into safety failures."

    Every help will be appropriate!
    Thanx!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Joshua meant this:

    Java Code:
    class Alien implements Runnable  {
    
    	private T t;
    	public Alien(T t) {
    		this.t= t;
    	}
    	
    	public void method() {
    		t.sync(this);
    	}
    	
    	public void run() {
    		t.syncToo();
    	}
    }
    
    public class T {
    
    	public static void main(String[] args) {
    		T t= new T();
    		Alien alien= new Alien(t);
    		alien.method();
    	}
    	
    	public synchronized void sync(Runnable r) {
    		r.run();
    	}
    	
    	public synchronized void syncToo() {
    		System.out.println("huh?");
    	}
    }
    A class T object synchronizes both of its methods on 'this'. An Alien object calls the sync() method which runs the Runnable. Back in Alien it can call another method in T that also attempts to synchronize on 'this' but fails ...

    kind regards,

    Jos

  3. #3
    Jackson86 is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Ok. I understood. Very good example! )

    Thank you very much for your answer and your time.

Similar Threads

  1. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM
  2. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM
  3. Replies: 0
    Last Post: 12-23-2008, 12:27 PM
  4. Replies: 1
    Last Post: 12-17-2007, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •