user1147070
user1147070

Reputation: 491

What is use of synchronized method if we have synchronized block?

If I always call a method from a synchronised block then what is advantage of making any method as synchronized?

Thanks in advance.

Upvotes: 0

Views: 18

Answers (1)

Stewart
Stewart

Reputation: 18302

It's just another way of writing the same thing. If your synchronized block exactly matches a method block, then it's just easier to read that way. Syntax sugar.

Which is easier?

public synchronized void myMethod() {
        // do stuff
}

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

Upvotes: 1

Related Questions