It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes Monitor problem in RHE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Monitor problem in RHE" Watch "Monitor problem in RHE" New topic
Author

Monitor problem in RHE

Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
Hi,
Page 214 in RHE has the code for Consumer3, but the code in that version of the program isn't intended to call properly formed thread methods. Below, is my version of Consumer3 calling the methods in Mailbox3.
How am I going to call the synchronized methods in Mailbox3? I can't call them with an instance of Thread because Thread doesn't have those methods. It's no use to make Mailbox3 a Runnable because it doesn't use the run() method. And I can't use the run() method because run() won't accept anything passed into it.
Thanks!
import java.lang.*;
public class Consumer3 extends Thread {
Mailbox3 m3 = new Mailbox3();
Thread t = new Thread(m3);
Thread t2 = new Thread(m3);
public void run() {
t.meth2();
t2.meth1("hey everybody");
}
public static void main(String args[]) {
Consumer3 c3 = new Consumer3();
c3.start();
}
}
import java.lang.*;
class Mailbox3 implements Runnable {
private boolean request;
private String message;
public void run();
public synchronized void meth1(String message) {
while(request == true) {
try {
wait();
} catch (InterruptedException ie) {
}
}
request = true;
this.message = message;
notify();
}
public synchronized String meth2() {
System.out.println("I'm in meth2()");
while(request == false) {
System.out.println("request = this in meth2() " + request);
try {
wait();
} catch (InterruptedException e) { }
}
request = false;
notify();
return message;
}
}
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Why don't I move this to the Thread Forum where they discuss stuff like this all the time.
Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
Why don't I move this to the Thread Forum where they discuss stuff like this all the time;
You mean like in my other topic 'Question on thread in RHE', where I'm asking the same question, with no responses? If I could just get an idea of what is wrong with the question, I'll alter it in whatever way necessary. Thanks.
[ February 18, 2002: Message edited by: Douglas Wolfinger ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, the first reason for the lack of responses is probably the fact that many of us find it tiresome to try to understand unformatted code. Without any indentation to clarify the structure, we need to spend that much more time looking at each and every { and }. It's an extra layer of difficulty that discourages people from wanting to spend much time with your code. You can fix this by indenting to show structure, and then using the [ code ] and [ /code ] tags (without the internal spaces) around your code to preserve the format. (See tag explanations here.)
The other main reason I didn't reply earlier is that I really couldn't tell what you were trying to do, and was hoping someone else would know. In RHE, the Consumer class is extended from Thread, but Mailbox is not - and there's really no reason it needs to be. If the Consumers run in threads of their own, the Mailbox can just sit there untill called on by a Consumer (or other Thread) It doesn't really need a thread of its own. Its methods needed to be made synchronized in order to be thread-safe, but that's not the same thing as being a Thread itself. See subsequent pages in RHE for elaborations on how Mailbox can be modified constructively.
Assuming there's some need for the Mailbox to be a Thread (or Runnable) - what do you want it to do? That's not clear to me. Regardless, if you want to pass data into the Mailbox before its thread runs, you would need to create Mailbox methods which you can call to pass the data, before run() is executed.
[ February 18, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
Thanks so much for the reply.
This is the closest I have come to getting it to do what I want. It goes back and forth a couple times between the three methods in Mailbox3, but wait() never gets called in either of them. I also made a slightly altered version where the message is passed into another method, but control never escapes from the wait() in meth2().
I think the intent by the authors was to have both meth1() and meth2() be called by threads. And if my run() method is in Consumer4, I don't know how to call Mailbox3's methods without simply calling them with an instance of Mailbox3. In that case, they aren't threads, they're just ordinary method calls.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Douglas Wolfinger:
Thanks so much for the reply.
This is the closest I have come to getting it to do what I want.


Can you clarify exactly what your goal is here? what are you trying to do? I'm not really clear on the question.
Thanks.


Rob
SCJP 1.4
Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
The version of Mailbox I have posted below is the version the RHE authors say is the complete Mailbox class. Since it is a monitor class, I want to execute the synchronized methods with threads. But Thread.class doesn't have these methods so I can't just say
Thread t = new Thread();
Thread t2 = new Thread();
t.retrieveMessage();
t2.storeMessage("a message");
So, as shown in my previous message, I've altered the version of Mailbox the authors say is complete, and made it a Runnable and put in a run() method. In Consumer, I pass the Runnable object into 2 thread instances, and use those to start run() in Mailbox. But it still doesn't work like a monitor should because wait() isn't being executed in either method. I would like to know how to execute the Mailbox code below with threads. Thanks again!
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
A "monitor" is just an object that owns a lock, that's all.
Here's an application that will create two threads, one a producer, and one a consumer, and use your mailbox to share information between them.
Hope this is what you are looking for.
Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
Thank you, much appreciated! I haven't had much time to study your code, which is considerably different than my Consumer4.java. (It's interesting that you're assigning a String the value of the method retrieveMessage().) However, wait() never gets called in either Mailbox method, same as in my version. And that whole section of ch. 7 was about wait() and notify().
They explain the concepts well, but it seems strange that they would include fragmented code that doesn't actually run, if that is the case. I'm sure I have come across a program with wait() and notify() that runs, so I'll look for such a program and play with it.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Why do you say wait() and notify() don't get called? Of couse they get called!
When the consumerThread starts, the first thing it does is try to retrieve a message. Since there are no messages in the mailbox yet, the first thing it does is call wait() and that thread blocks.
Eventually, the producerThread will store a message in the mailbox and call notify() which will wake up the blocked consumerThread, and the producerThread then calls wait() until the consumer has picked up it's message.
[ February 21, 2002: Message edited by: Rob Ross ]
Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
yep, you beat me to it. I was coming back in to say that I put diagnostic prints in the try blocks, and of course they printed. Originally I thought the long value I put in the wait(5000) would slow it down, but it didn't, so I thought the waits() weren't being executed.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
calling wait(5000) means you want to wait no more than 5 seconds. It doesn't say anything about a minimum wait time. If you want to slow a thread down try using sleep() instead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Monitor problem in RHE