0 votes
in JAVA by
What's a monitor in Java?

What's a monitor referred to in concurrent programming in Java?

When I read that "every object has associated a monitor" what does it meaning?

Is it a special object?

1 Answer

0 votes
by
A monitor is mechanism to control concurrent access to an object.

This allows you to do:

Thread 1:

public void a()

{

    synchronized(someObject) {

        // do something (1)

    }

}

Thread 2:

public void b()

{

    synchronized(someObject) {

        // do something else (2)

    }

}

This prevents Threads 1 and 2 accessing the monitored (synchronized) section at the same time. One will start, and monitor will prevent the other from accessing the region before the first one finishes.

It's not a special object. It's synchronization mechanism placed at class hierarchy root: java.lang.Object.

There are also wait and notify methods that will also use object's monitor to communication among different threads.

Related questions

+3 votes
asked May 13, 2021 in JAVA by rajeshsharma
0 votes
asked May 7, 2021 in JAVA by sharadyadav1986
...