Q: Java synchronized blocks object vs this Difference between the two synchronized blocks in the doThis method:
Which should be used when?
public class AClass {

private final Object lock = new Object();

public void doThis(){
    synchronized(lock){
        //do stuff
    }
}

}

and
public class BClass {

public void doThis(){
    synchronized(this){
        //do stuff
    }
}

}

When should one be used over the other?

A: Always use the first one.
If you do synchronized (this), you are synchronizing on an object that any other code also has the ability to synchronize on, and thus unwittingly mess up your class's ability to function.
If you synchronize on a private object, absolutely no one can synchronize on it except the code in your class.  No one has the power to interfere with your functionality.

A: The reason to use a lock object, rather than this, is simply to give yourself flexibility and to avoid deadlocks. Note that these two reasons only come into play if different methods need to synchronize, and not against each other. Providing two lock objects then makes sense. Most of the time, just using this is fine.
