2

I grabbed the concept of synchronization as the following: every object in Java has a monitor and a lock. Lock is an entity which can be acquired or released by a thread, whereas monitor is an instance's mechanism which decides whether or not a thread can get the lock on an object.

I'd like to clarify the following example. Firstly, I want to create two objects:

  MyClass obj1 = new MyClass();
  MyClass obj2 = new MyClass();

Will each instance has its own monitor and lock? Or a shared one?

If I synchronize on a class:

  synchronized (MyClass.class) {
     ...
  }

How will it behave? Will this synchronization block acquiring of obj1 and obj2 locks? If so, is there any lock hierarchy in Java?

Follow-up: Which is the correct way to say "the object's monitor" or "the object's lock"?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
void
  • 731
  • 2
  • 11
  • 26
  • "Will this synchronization block acquiring of obj1 and obj2 locks" No. It only acquires the lock on `MyClass.class`, the instance of `Class`, which is returned by `obj{1,2}.getClass()`. – Andy Turner Nov 19 '15 at 13:27
  • see this : http://stackoverflow.com/questions/14495776/synchronizethis-vs-synchronizemyclass-class, it answers your question. – Paul K. Nov 19 '15 at 13:29
  • monitor and lock are the same thing, see https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html – Nathan Hughes Nov 19 '15 at 13:56

2 Answers2

2

every object in Java has a monitor and a lock.

That doesn't sound right. Every object has a lock. Some people call the lock a "monitor". Monitors are an idea out of the past that never gained much traction, but back when Java was created, the implementers thought that monitors would be the next Big Thing. Basically, a monitor is an object whose methods are all synchronized.

Will each instance has its own... lock?

Yes.

If I syncronize on a class ... How will it behave?

A class is an object.

There are three distinct objects in your example; obj1, obj2, and MyClass.class. Each of those objects has its own lock, and their locks do not interact with one another at all.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • Am I right that if I want to sync access to a static field or method, then I should use `synchronized (MyClass.class)` ? If so, then will it sync non-static members of a class as well? – void Nov 19 '15 at 14:02
  • @Anton The only thing that matters is, if you want different threads to synchronize their access to the same shared data, then it's _up to you_ to make sure that all of the threads synchronize on the same object. If the data you want to protect is accessed via a `static` field of some class, then accessing the lock via a `final static` field of the same class is an obvious way to achieve that. – Solomon Slow Nov 19 '15 at 14:05
0

If I synchronize on a class... How will it behave? Will this synchronization block acquiring of obj1 and obj2 locks?

If you synchronize on MyClass.class it will only sync with other attempts to sync on MyClass.class. So locking on obj1 or obj2 will have no effect.

If so, is there any lock hierarchy in Java?

Nope, there is no lock hierarchy in Java.

Which is the correct way to say "the object's monitor" or "the object's lock"?

Typically people only refer to "the object's lock" when talking about synchronization. Monitor's come into play when you start using wait() and notify(), but that is a different topic.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61