-2

Probably a very basic question but I'm still in doubt. Am I correct in assuming that

public synchronized void doSynchronized() {
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

Is exactly the same as:

public void doSynchronized() {
        synchronized (this) {
            for (int i = 0; i < 10000; i++) {
                count++;
            }
        }
    }

???

BRNTZN
  • 564
  • 1
  • 7
  • 21
  • Yes both remain same. – Vivek Singh Dec 04 '15 at 10:36
  • 1
    [Java Language Specification §17.1](https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.1) *"A `synchronized` method (§8.4.3.6) automatically [..] locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as `this` during execution of the body of the method)"* – zapl Dec 04 '15 at 10:43

1 Answers1

0

Yes.

Everything else in this answer is just because it hast to be at least 30 characters to be submitted.

TwoThe
  • 13,879
  • 6
  • 30
  • 54