0

I'm just starting with synchronization in Java and I have small question.

Is this method:

public synchronized void method() {
    // ... do staff ...
}

Is equal to:

public void method() {
    synchronize(this) {
        // ... do staff ...
    }
}

PS

Recently I watched 2 good confs about Java (and from this comes my question)video 1, video 2. Do you have some relative videos (I'm interested in programming in Java and Android).

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2365209
  • 99
  • 1
  • 1
  • 9
  • http://stackoverflow.com/questions/574240/synchronized-block-vs-synchronized-method – Suresh Atta Jul 27 '13 at 10:07
  • 1
    You need to attend some conferences, but this is not a conference hole. – Roman C Jul 27 '13 at 10:08
  • (1) Yes. (2) You don't need any videos to establish this, just the existing documentation. (3) If you think you're going to learn computer programming for videos, you have another think coming. – user207421 Jul 27 '13 at 10:21

5 Answers5

0

Yes. Also this:

public static synchronized void method() {
}

is equivalent to:

public static void method() {
    synchronized (EnclosingClass.class) {
    }
}

Regarding videos, just search "java synchronization" on youtube.

Keith
  • 4,144
  • 1
  • 19
  • 14
0
public void method() {
    synchronize(this) {
        // ... do staff ...
    }
}

is semantically equivalent to

public synchronized void method() {
    // ... do staff ...
}
Vikas V
  • 3,176
  • 2
  • 37
  • 60
0

Yes it is. synchronized methods are implicitly synchronized over the instance to which the method belongs.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
0

Method:

public synchronized void method() { // blocks "this" from here.... 
    ...
    ...
    ...
} // to here

Block:

public void method() { 
    synchronized( this ) { // blocks "this" from here .... 
        ....
        ....
        ....
    }  /// to here...
}

Blocks do have advantages over methods, most of all in flexibility. The only real difference is that a synchronized block can choose which object it synchronizes on. A synchronized method can only use 'this' (or the corresponding Class instance for a synchronized class method).

synchronized blocks are more flexible since it can compete for the associated lock of any object, often a member variable. It's also more granular because you could have concurrent code executing before and after the block but still within the method. Of course, you could just as easily use a synchronized method by refactoring the concurrent code into separate non-synchronized methods. Use whichever makes the code more comprehensible.

Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

Community
  • 1
  • 1
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

The code outside synchronized block can be accessed concurrently by multiple threds.

public void method(int b) {
    a = b // not synchronized stuff
    synchronize(this) {
        // synchronized stuff
    }
}

This will always be synchronized :

public synchronized void method() {
    // synchronized stuff
}
VishalDevgire
  • 4,232
  • 10
  • 33
  • 59