• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

monitor question

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Java monitor must either extend Thread or implement Runnable. Ture or False
The answer is False. The explanation is: A monitor is an instance of any class that has synchronized code.
As monitor is supposed to be started as thread(s) and in order to be started as a thread, the object must be either an instance of a subclass of Thread class or an instance of any class which implements Runnable interface. But what's the purpose of a monitor which neither extends Thread class nor implements Runnable interface?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin
As I understand it, there is no actual monitor object. An objects monitor is a collection of all of the synchronized code that that object has. You can't create (not directly anyway) a monitor or work with one. When you create an object that has synchronized code that object has a monitor associated with it. The monitor then has control over all of the syncronized code belonging to the object and controls what gets access and what has to wait for the lock to be released. Think of it like a gate that only lets one thing in at a time. When one is done another can enter, but it doesn't have any physical representtation. I hope that helps even if only a little. If I off base on this someone should be able to explain it better.
Dave
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dave,
In "complete Java 2 Certification Study Guide", I find the following sentences:
A monitor is an object that can block and revive threads.
A monitor is any object that has some synchronized code.
How should I undersand these?
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allen Holub talks about monitors and threads in this article in JavaWorld.
If you want a more technical discussion, see the original article by C.A.R. Hoare.
The book is right, a monitor is any object that has synchronized code. The monitor's job is to ensure that only one thread can execute the synchronized code at any given time. It has nothing to do with being a subclass of Thread or implement Runnable, although monitors are used with Thread or Runnable type objects.
HTH,
Junilu

Originally posted by Bin Wang:
Hi dave,
In "complete Java 2 Certification Study Guide", I find the following sentences:
A monitor is an object that can block and revive threads.
A monitor is any object that has some synchronized code.
How should I undersand these?



[This message has been edited by JUNILU LACAR (edited May 30, 2001).]
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does a thread execute the synchronized code of a monitor if the monitor class neither extends Thread nor implements Runnable?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bin Wang:
How does a thread execute the synchronized code of a monitor if the monitor class neither extends Thread nor implements Runnable?


Bin
There is no actual Monitor class (at least not that I've ever heard of). Think of it this way:
You have a class that has several instance methods that are marked as synchronized. When you create an instance of the class all of the synchronized methods are wrapped up by the monitor.
A decent analogy would be a line at a bank. Imagine there are a lot of people (these are the threads) in several different lines at a bank. All of the lines lead to the same place, however there is only one person there to help all of these people (this is the monitor). Only one person can get in at a time and when they are done another can enter. That may not be the best explaination, the site Junilu mentioned has a good article on it.
There isn't an actual monitor object, every object that has synchronized code has a monitor that just makes sure only one thread at a time can get in.
If that made it worse sorry, someone else may be able to give a better explaination.
Dave
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bin Wang:
How does a thread execute the synchronized code of a monitor if the monitor class neither extends Thread nor implements Runnable?


Same way you execute regular code: invoke the synchronized method or the method that contains synchronized code.
See the Producer/Consumer example in the Java Tutorial.
Junilu

 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
I know there is no Monitor class and I understand your explanation about the lines at the bank. But my question is: how does a thread execute the synchronized methods? Do the synchronized methods must be called within the run() method?
Thanks.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "Monitor" is any object that can block/ revive threads (definition). That is, any object containing synchronized code can be a monitor in Java.
So the monitor is not the thread, it is the object which is in some way invoked by a thread (typically by calling a synchronized method).
The synchronized code/ methods of the monitor are indeed usually called from the run() method of a thread. If the synchronized method of the monitor then has a call to wait(), this blocks the calling thread...
Hope this helps, I was confused myself.
Ranjan
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranjan,
If the synchronized code of the monitor will be called in the run() method of a thread, must it(the monitor) implement the Runnable interface?
I mean how can you bind the monitor with a thread if it(monitor) neither extends the Thread class nor implement the Runnable interface?
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bin,


Entities possessing both locks and wait sets are generally called monitors
Concurrent Programming in Java, 2nd Edition: Design Principles and Patterns p184


You can say an object has a lock and can be manipulated by using <code>wait(), notify(), notifyAll(),</code> or <code>Thread.interrupt</code> or you can say the object is a monitor. It's a descriptive term.


JLS �17
The Java programming language supports the coding of programs that, though concurrent, still exhibit deterministic behavior, by providing mechanisms for synchronizing the concurrent activity of threads. To synchronize threads, the Java programming language uses monitors, which are a high-level mechanism for allowing only one thread at a time to execute a region of code protected by the monitor. The behavior of monitors is explained in terms of locks; there is a lock associated with each object.


JLS �17 explains how the JVM handles multi-threading by means of the synchronized keyword and montiors.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited May 31, 2001).]
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A monitor does not have to be a subclass of Thread or implement Runnable.
Think of the monitor as the door to a restroom stall. The door has a lock. Even though many people (think of them as threads) may be in line to use the stall, only one of them can do so at a time. The person who is using the stall has control of the lock on the door and the stall. The sole purpose of the monitor (the door) is to control access to the synchronized code/method (the stall) by threads (the people).
When one person uses the stall, all other people are locked out and have to wait until the door is unlocked and the person currently using the stall exits. Similarly, when one thread gets a lock on a monitor, all other threads have to wait until the current thread gives up that lock.

[This message has been edited by JUNILU LACAR (edited May 31, 2001).]
 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bin Wang:
If the synchronized code of the monitor will be called in the run() method of a thread, must it(the monitor) implement the Runnable interface?
I mean how can you bind the monitor with a thread if it(monitor) neither extends the Thread class nor implement the Runnable interface?



My previous message went astray... but here is some code to (hopefully) clarify what the others have said.
Compile and run in file Test.java
------------------------------------------
class Monitor {

public synchronized void printOutMsg(String out) {
System.out.println(out);
System.out.println("Pausing thread...");
try {
wait();
} catch (InterruptedException ie) { }
System.out.println("Resuming thread...");
}

public synchronized void notifyThreads() {
System.out.println("Notifying all threads.");
notifyAll();
}
}

class Test {
static Monitor m = new Monitor();

static class MsgPrinter extends Thread {
public void run() {
m.printOutMsg("BOO!");
System.out.println("End MsgPrinter run() method.");
}
}

static class Notifier implements Runnable {
public void run() {
try {
Thread.sleep (5000);
} catch (InterruptedException ie) { }
m.notifyThreads();
System.out.println("End Notifier run() method.");
}
}

public static void main(String args[]) {
MsgPrinter msgPrinter = new MsgPrinter();
msgPrinter.start();
Notifier notifier = new Notifier();
Thread notifierThread = new Thread(notifier);
notifierThread.start();
}
}
---------------------------------------
Here the two threads each call the synchronized code in the monitor object via their respective run() methods.
Hope this helps rather than obfuscates!
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should say that Ranjan's program is worth thousand words. Now I understand it.
Ranjan, in your test class, you create two static inner member classes, right? Thank you very much!
Also, thank you, Dave, Junilu and Jane, you are great!
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Although it's not necessary for a monitor to extend Thread class or implement Runnable interface. It's legal for a monitor to extend Thread class or implement Runnable interface, right?

 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bin Wang:
Ranjan, in your test class, you create two static inner member classes, right? Thank you very much!


Yes, otherwise they could not be instantiated in the main() method. Also, yes, there's no reason why a "monitor" cannot also be a thread (or a thread cannot be a "monitor") if required.
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic