Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object synchronization

Tags:

java

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?

like image 401
user961690 Avatar asked Sep 23 '11 18:09

user961690


People also ask

What is object synchronization?

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.

How do you explain synchronization?

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.

What are the types of synchronization?

There are two types of synchronization: full and incremental.

What is synchronization with example?

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.


2 Answers

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)

like image 200
Dilum Ranatunga Avatar answered Oct 27 '22 23:10

Dilum Ranatunga


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.

like image 41
Gabe Avatar answered Oct 28 '22 00:10

Gabe