When two or more threads need access to .1 shored resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called  As you will see, Java provides unique, language-level support for it. .
Kcy to synchronization is the concept of the monitor (also called a semaphore). A monitor is an object that is used as a mutually exclusive lock, or muiex. Only one thread can onm's monitor at eventime. When a thread acquires a lock, it is said to have entered ssw monitor. All-other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor. These other threads arc said to be waiting for the monitor. A thread that owns a monitor can reenter the same monitor if it . so desires. . .
If you have worked with synchronization when using other languages, such as C or C++, you know that it can be a bit tricky to use. This is because most languages do not,themselves, support synchronization. Instead, to synchronize threads, your programs need to utilize operating system primitives, Fortunately, because java implements synchronization through language elements, mosl of the complexity associated with synchronization has been eliminated.You synchronize your code in either of two ways. Both involve the use of the synchronized keyword, and both arc! examined here.

Share This