File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Explanation For A Thread Analogy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Explanation For A Thread Analogy" Watch "Explanation For A Thread Analogy" New topic
Author

Explanation For A Thread Analogy

Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi all ,

The analogy that I came across when I read a article form the internet.The article specifies that analogy for monitors in threading is similar to bathroom in a airplane.it explains that airplane is equal to object ,passengers are same as threads and bathroom is identical to monitor. doors attached to the bathroom is like locks.The story keep going like this. When passenger enter to the bathroom he locks the door. Meaning that thread acquire the lock. Other passengers won't be able to use the bathroom since bathroom's doors are locked.

What I need to know is what does monitor do and what does object lock do?. does object associates with particular lock and a monitor.


Thanks & Regards,
Nuwan Arambage



Thinker
Nuwan Arambage
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Threads are always more about understanding than coding...! I have only one article for you , but this is not directly for monitoring of object, instead its about

difference between WAITING and YIELDING Part 1.
difference between WAITING and YIELDING Part 2

I hope this would help for you.

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Sergey Babkin
author
Ranch Hand

Joined: Apr 05, 2010
Posts: 50
It's a bit confusing. There are two common meanings to the word "monitor". First, "monitor" is an antique word that means the same as modern "mutex" which is also the same as "lock" (noun). Note that the word "lock" can be used both as a verb and as a noun: you can lock a mutex and unlock a mutex, and lock a lock, and unlock a lock.

Second, in Apache code "monitor" means a combination of a mutex and a condition variable (which is an object that you can use for wait()/notify()). So, technically, the Java objects are monitors in this sense, since you can do both synchronized() (e.g. lock/unlock) and wait()/notify() on them.

And, I guess, third meaning of "monitor", used in this book, is the same as is commonly called "critical section".

Because of this confusion, the word "monitor" is best not to be used.

So, the airplane lavatory is a "critical section": place that normally can be occupied by only person at a time. The lock on the lavatory's door is a "mutex": a device that let's the current occupant to be undisturbed after it's locked and until it's unlocked. If other passengers want to use the lavatory, they find the door locked and queue up waiting for it to get unlocked, taking their turns.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 294


Dude don't confuse yourself too much , in general sense monitor and lock used for same purpose to provide mutual exclusive access to a critical section code.l I found monitor word mostly in C , C++ while Lock mostly in Java.

Also in Java every object has a lock which is used to enter critical section.

Thanks
Javin


SQL Query Interview Question
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
yeah , conceptually it's dude. I got you point clearly thanks for you guidance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explanation For A Thread Analogy