Because multithreading introduces an asynchronous behavior to your programs, there must be a way for you to enforce synchronicity when you need it. For example, if you want two threads to communicate and share a complicated data structure, such as a linked list, you need some way to ensure that they don’t conflict with each other. That is, you must ‘prevent one thread from writing data while another thread is in the middle of reading it. For this purpose, Java implements an elegant twist on an age-old model of interprocess synchronization: the monitor.

The monitor is a control . mechanism first defined by CAR. Hoare. You can think of” monitor LIS a very small box that can hold only one thread. Once a thread enters a monitor, all other threads must wait until that thread exits the monitor. In this way, a monitor can be used to protect a shared asset from being manipulated by morethan one thread LIt a time. Most multithreaded systems expose monitors as objects that your progrnm must explicitly acquire and manipulate. lava provides a cleaner solution. There is no class “Monitor”; instead, each object has its own implicit monitor that is automatically entered when one of the object’s synchronized methods is called, Once a thread is inside a synchronized method, no other . thread ca call nny other synchronized method on the same object. This enables you to write very clear and concise multithreaded code, because synchronization built in to the language.

[vfb id=1]

Share This