views:

117

answers:

4

I read this in Java language Spec 17.1:

"Each object in Java is associated with a monitor, which a thread can lock or unlock."

Why necessarily? Doesn't that make java object too heavy weight? I've no idea why a Object like, say, a string, should be naturally a monitor!

EDIT:

I think it over and yes, Java has a keyword synchronized, because EVERY object could have a synchronized method, so it's necessary to associate EVERY object a Monitor.

But still this seems not a very good solution, usually you need more that one mutex for one class, except for that pojo classes that's really very simple.

A: 

Thread synchronization is necessary to ensure correct outcome as their may be racing condition.

Well, if it is not as heavy as a LCD or CRT monitor, it would be fine?

Nirvana6
Things can be necessary, doesn't mean they should be everywhere.
tactoth
+1  A: 

It's a pretty smart way to make virtually everything thread-safe. I think heavy weight is somewhat subjective; in Java, for example, the object only gains a notifiable wait queue, while specifying mutual exclusion is explicitly done with synchronize.

C# uses a similar method to enforce thread safety, so clearly MS also thought it was a pretty clever solution. The alternative is what? Hand-written semaphores and mutexes? In Java, that would be a nightmare, considering most production-level apps (i.e. servers, services, etc) have to be multi-threaded. Having the language do all that tough/boring stuff for you is kind of awesome.

David Titarenco
Seems this a monitor is not assigned for C# objects. Microsoft is doing it differently. See the link below. Microsoft use separate class Monitor for thread synchronization.
tactoth
http://msdn.microsoft.com/en-us/library/system.object_members.aspx
tactoth
Read this: http://www.shannonhardt.com/ThreadingInJavaCsharp.pdf
David Titarenco
In Java, this support is in three main forms:a mutex variable is associated with every object; there is a set of methods defined on Object thatsupport coordination of threads; and a set of classes in java.lang that allows programmers tocreate and manage threads. Similarly, in .NET there is a Monitor (or lock) associated with everyobject instance. The System.Threading namespace contains classes that allow thread creation andmanipulation.
David Titarenco
...Yes you are right. C# does assign a monitor for all objects.
tactoth
+1  A: 

not that heavy in weight. objects are cheap.

I agree that the concept that any object can be a lock is quite confusing. many people thinks that synchronized(obj) protects the obj from being accessed concurrently. if we have separate locks from states, this misconception is less likely.

not text in java memory model shows any importance of using arbitrary object, or any object, as synchronization primitives. maybe it's economical design to use objects for this purpose.

it could as well use integers as locks. synchronized(493725) actually since each object is associated with an integer internally (its address), JVM probably does this. There's zero overhead for objects that are not being synchronized upon.

with java.util.concurrent classes, you don't ever need such synchronized(obj) any more, if you dislike it.

irreputable
Thank you for sharing that knowledge. However I'm confused how it's implemented in the underlying OS level? With windows API to create a mutex I'll have to call "CreateCriticalSection" to get a handle, so I'm wondering does JVM create one critical section for each object? That sounds crazy!
tactoth
JVM doesn't need OS for mutex, it can have its own impl. It allocates necessary locking data structure for an object *only* when `synchronized(obj)` is executed. for objects that are never `synchronized`, no need to do anything.
irreputable
+2  A: 

Your fundamental problem is in assuming that every object has some sort of Monitor built into it, waiting for it to be used by some code. In reality, most objects are never used as a monitor, so the monitors don't have to be created until they are used. Rather than implementing this feature as every object having a private Monitor monitor field, think of it as being implemented as the JVM having a global HashMap<object, Monitor> monitors.

A possible implementation is this: Whenever a synchronized block is entered, the JVM looks up the synchronized object in the map (monitors). If it finds it, it gets the monitor to use. If it doesn't find it, it enters a critical section dedicated to the map. It then looks up the object again because another thread may have created it between the previous check and entering the critical section. If it's still not there, it creates the monitor for the synchronized object and leaves the critical section.

Gabe
I agree in this way it could work, although still not sure if it really work in this way.
tactoth
As far as I know, this strategy was used in older JVMs but was discarded because it creates a bottleneck as you must synchronize access to the same `HashMap` for *every* monitor. Nowadays it is used when you need to trade speed/parallelism for space efficiency.
gpeche