This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes explain the output of this example that uses threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "explain the output of this example that uses threads" Watch "explain the output of this example that uses threads" New topic
Author

explain the output of this example that uses threads

William Quantrill
Ranch Hand

Joined: Dec 23, 2002
Posts: 36
Im trying to understand the output from this sample from Oreilly's "learning Java"
my questions:
- why does the output change after the 11th message, im sure it has something to do with the fact that the vector limit is 5 message
- after the message vector fills up, are messages that are printed from then on only the ones "on the top" of the vector, meaning that the others deeper in never to be shown ?

From what i gather looking at the output, a message is printed every 2 seconds(even though there is a new message available ever 1 second), untill 11 messages have been printed. then the message is different. notice the output on the 12th message is now different in the time value displayed. each message is now 2 seconds apart versus 1 second.
in the main method, a producer is started.
the producer puts a message and sleeps for 1sec
the consumer gets a message and sleeps for 2 seconds
hence the printing of the message ever 2 seconds

- - - Here is the code - - -
package app_tester;

//file: Consumer.java
import java.util.Vector;
class Producer extends Thread {
static final int MAXQUEUE = 5;
private Vector messages = new Vector( );

public void run( ) {
try {
while ( true ) {
putMessage( );
sleep( 1000 );
}
}
catch( InterruptedException e ) { }
}
private synchronized void putMessage( )
throws InterruptedException {
while ( messages.size( ) == MAXQUEUE )
wait( );
messages.addElement( new java.util.Date().toString( ) );
notify( );
}
// called by Consumer
public synchronized String getMessage( )
throws InterruptedException {
notify( );
while ( messages.size( ) == 0 )
wait( );
String message = (String)messages.firstElement( );
messages.removeElement( message );
return message;
}
} // end of class Producer
public class Consumer extends Thread {
Producer producer;
Consumer(Producer p) {
producer = p;
}
public void run( ) {
try {
while ( true ) {
String message = producer.getMessage( );
System.out.println("Got message: " + message);
sleep( 2000 );
}
}
catch( InterruptedException e ) { }
}
public static void main(String args[]) {
Producer producer = new Producer( );
producer.start( );
new Consumer( producer ).start( );
}
}

- - - output - - -
Got message: Wed Dec 25 19:11:57 PST 2002
Got message: Wed Dec 25 19:11:58 PST 2002
Got message: Wed Dec 25 19:11:59 PST 2002
Got message: Wed Dec 25 19:12:00 PST 2002
Got message: Wed Dec 25 19:12:01 PST 2002
Got message: Wed Dec 25 19:12:02 PST 2002
Got message: Wed Dec 25 19:12:03 PST 2002
Got message: Wed Dec 25 19:12:04 PST 2002
Got message: Wed Dec 25 19:12:05 PST 2002
Got message: Wed Dec 25 19:12:06 PST 2002
Got message: Wed Dec 25 19:12:07 PST 2002
Got message: Wed Dec 25 19:12:09 PST 2002
Got message: Wed Dec 25 19:12:11 PST 2002
Got message: Wed Dec 25 19:12:13 PST 2002
[ December 26, 2002: Message edited by: William Quantrill ]

"The Rebel army is now the legitimate property of the Army of the Potomac." - Joseph Hooker spoke these pompous words shortly before he was soundly defeated by Robert E. Lee at Chancellorsville
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
I am confused too. Is there anyone so kind to explain a little bit please? Thanks in advance.
Rajakumar Makapur
Greenhorn

Joined: Jun 23, 2001
Posts: 13
Its Simple.
The producer starts putting messages into the Vector for evey one second and It will go in wait only if the Vector is full that is MAX(5 in your case).
The first message is at Wed Dec 25 19:11:57 PST 2002 after each second it puts message into the Vector so the first 5 messages are
Got message: Wed Dec 25 19:11:57 PST 2002
Got message: Wed Dec 25 19:11:58 PST 2002
Got message: Wed Dec 25 19:11:59 PST 2002
Got message: Wed Dec 25 19:12:00 PST 2002
Got message: Wed Dec 25 19:12:01 PST 2002

The Consumer starts getting messages from the Vector for evey two second and It will go in wait only if the Vector is empty.
First time it reads the message and removes from the Vector and it does same after every two seconds.
Here in this case Producer puts 9 messages after the 9 secs(1 secs interval) and Consumer is consumed 4 messages after 9 secs(2 secs interval) and its in sleep mode. So still 5(MAX) message are there in Vector.
When producer comes to put 10th message at 10 secs the Vector is full thatz y it goes into wait and the 10th message is not there in output, then comes the Consumer at its 10th sec and reads the 5th message and removes it from vector and notifies the Producer. The producer will put the 10th message at 11th Sec. and again it will go into wait for next try.
So here afterwords the Producer will go into sleep mode for every alternate secs.So the messages are
Got message: Wed Dec 25 19:12:09 PST 2002
Got message: Wed Dec 25 19:12:11 PST 2002
Got message: Wed Dec 25 19:12:13 PST 2002
In this case The Consumer will probably go into wait mode only first time when it starts if and only if the Producer is not yet put the message into the Vector before the Consumer tries to read it.
Hope this will help you to understand the sample.
luv
Raja.
[ January 13, 2003: Message edited by: Rajakumar Makapur ]
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Hi,Rajakumar,
got it. Thank you very much!

Regards,
Ellen
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: explain the output of this example that uses threads