Вы находитесь на странице: 1из 4

7/14/13

explain the output of this example that uses threads (Threads forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Threads and Synchronization

Author

explain the output of this example that uses threads


posted 12/26/2002 9:24 PM

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 - - -

www.coderanch.com/t/232128/threads/java/explain-output-threads

1/4

7/14/13

explain the output of this example that uses threads (Threads forum at JavaRanch)

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 ) { } }
www.coderanch.com/t/232128/threads/java/explain-output-threads 2/4

7/14/13

explain the output of this example that uses threads (Threads forum at JavaRanch)

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 C hancellorsville

Ellen Zhao Ranch Hand Joined: Sep 17, 2002 Posts: 581 Rajakumar Makapur Greenhorn Joined: Jun 23, 2001 Posts: 13

posted 1/14/2003 12:31 AM

I am confused too. Is there anyone so kind to explain a little bit please? Thanks in advance.

posted 1/14/2003 11:24 AM

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.

www.coderanch.com/t/232128/threads/java/explain-output-threads

3/4

7/14/13

explain the output of this example that uses threads (Threads forum at JavaRanch)

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

posted 1/14/2003 11:45 AM

Hi,Rajakumar, got it. Thank you very much! Regards, Ellen

I agree. Here's the link: http://aspose.com/file-tools

subject: explain the output of this example that uses threads

Similar Threads Important, about threads Newbie Producer Consumer Problem In reference to notify maybe pausing a thread? threading access Explain me this output (notify)
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:39:20 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/232128/threads/java/explain-output-threads

4/4

Вам также может понравиться