Does synchronized(this) imply that the current thread object acquired its own lock?

java, multithreading

Solution

The current thread acquires the lock on the associated instance of the `MyThread` class.

The `synchronized(this)` is locking the same object as `synchronized(m)` in `main()`.

Finally,

public void run() {
    synchronized (this) {

is exactly equivalent to

public synchronized void run() {

Problem

Consider the following piece of code - ``` class MyThread extends Thread { private int x = 5; public void run() { synchronized (this) // <-- what does it mean? { for (int i = 0; i < x; i++) { System.out.println(i); } notify(); } } } class Test { public static void main(String[] args) { MyThread m = new MyThread(); m.start(); synchronized (m) { try { m.wait(); } catch (InterruptedException e) { } } } } ``` In the above example, does Thread m acquire the lock on itself?

Original source