When a synchronized block is used for synchronization on an object instance, then threads compete to get into object's implicit monitor. And once, any particular thread enters into the monitor, any other thread has to wait for entering into it. Then
synchronized(object){
// some code here
// no function call
}
must not enforce taking any particular type of object. Thus any object type can be used here because every object has its implicit monitor.
Kindly reply me is it true?
A synchronization object is an object whose handle can be specified in one of the wait functions to coordinate the execution of multiple threads. More than one process can have a handle to the same synchronization object, making interprocess synchronization possible.
Synchronization is the precise coordination of multiple events or mechanical devices. In computing, it refers to the coordination of hardware devices, such that the data they contain or provide is made to be identical. The synchronization is usually done in a short time frame.
There are two types of synchronization: full and incremental.
Synchronization is the coordination of events to operate a system in unison. For example, the conductor of an orchestra keeps the orchestra synchronized or in time. Systems that operate with all parts in synchrony are said to be synchronous or in sync—and those that are not are asynchronous.
Yes, every Java Object can act as a monitor.
And since this is such a short answer, for bonus, this is an interesting read: Does the JVM create a mutex for every object in order to implement the 'synchronized' keyword? If not, how?
Also note that C# does something similar with their objects, but also have value types (which are not Monitors)
Just keep in mind that if you have a variable that is null
, you cannot lock it. Also, while things like Integer
are objects, an int
or float
is not. You can lock an Integer
or int[]
, but not an int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With