What is a monitor?

What is a monitor?

- A monitor works as a lock on the data item.

- When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data.

- A unique monitor is associated with every object. These objects must be synchronized.

- They are also used to coordinate threads to make sure that they don’t trip over each other accessing the same data.

- It is a class which is used in the context of concurrency.

It supports two kinds of thread synchronization:

1. Mutual exclusion
2. Cooperation

1. Mutual exclusion:

- It is supported on the JVM via object locks.
- It enables multiple threads to independently work with shared data.

2. Cooperation:

- It is supported on the JVM via the wait() and notify() methods of class object.
- It enables threads to work together towards a common goal.
How do we allow one thread to wait while other to finish?
How do we allow one thread to wait while other to finish - Using the join() method, one can allow one thread to wait while other to finish....
Explain the purpose of yield method.
Explain the purpose of yield method - Yield method causes the currently executing thread object to temporarily pause and allow other threads to execute....
What is the difference between yielding and sleeping?
What is the difference between yielding and sleeping? - Sleep holds the threads execution for the specified time. On the other hand, yield will cause the thread to rejoin the queue....
Post your comment