This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Monitor - Multi Threading  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any one let me know what is this monitor in multi threading??
Thanks for your time
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Meenakshi Devi wrote:Hi,
Can any one let me know what is this monitor in multi threading??
Thanks for your time


Monitor is nothing but what we call 'lock' in multi-threading. It is a way of controlling the area in which only desired number of threads (mostly one) can enter. In OS lingo, it is also known as 'critical section' (in Java, it is the code which lies within synchronized block).

E.g. Vector is a thread-safe implementation of ArrayList. What do we mean by that? Let's check its add method(pseudo code):

Now, consider that two threads are adding two different objects to same list. What if the method (or the code which deals with adding the member) is not synchronized?
1) Currently, length is 0.
2) Thread A enters add method, increases the length. Length is now 1.
3) Thread B enters add method, increases the length. Length is now 2.
4) Thread A adds an object at list[1].
5) Thread B adds an object at list[1].
Thus, we have nothing at list[0], and one object at list[1]. One object (by one of the threads) is lost. This is what happens in ArrayList
This is where synchronization (and monitor) comes into picture. In Vector, we have:

Now, consider the same scenario which we took before:
1) Currently, length is 0.
2) Thread A acquires the monitor, enters add method, increases the length. Length is now 1.
3) Thread B tries to acquire the monitor, but it can't (since thread A owns it). So, it can't do any operations and would wait at line 2.
4) Thread A adds an object at list[0], gets out of critical section and releases the lock.
5) Thread B now can acquire the lock, enters in critical section, increases the length to 2, and adds an object at list[1].
6) Thread B gets out of critical section by releasing the lock on monitor. Now any other thread waiting on that lock can enter in critical section.
Thus, now we have list with length 2 and it also contains 2 objects.

I hope this helps.
 
Meenakshi Devi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Meenakshi Devi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have one more question regarding configuration of web logic... like an interviewer asked me "how do you configure weblogic??"
Thanks for your time.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Meenakshi Devi wrote:Hi
I have one more question regarding configuration of web logic... like an interviewer asked me "how do you configure weblogic??"
Thanks for your time.


Please do not ask new question on same thread(especially if it is unrelated to original post).

Also, this forum is for 'Threads and Synchronization'. There is a dedicated forum for Weblogic where you can get better and quicker help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!