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 Monitor - Multi Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Monitor - Multi Threading" Watch "Monitor - Multi Threading" New topic
Author

Monitor - Multi Threading

Meenakshi Devi
Greenhorn

Joined: May 31, 2011
Posts: 19
Hi,
Can any one let me know what is this monitor in multi threading??
Thanks for your time
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1507
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Meenakshi Devi
Greenhorn

Joined: May 31, 2011
Posts: 19
Thank you so much.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1507
    
    5

You are welcome.
Meenakshi Devi
Greenhorn

Joined: May 31, 2011
Posts: 19
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

Joined: Dec 08, 2010
Posts: 1507
    
    5

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.
 
Consider Paul's rocket mass heater.
 
subject: Monitor - Multi Threading