aspose file tools*
The moose likes Beginning Java and the fly likes Thread.sleep alters the output! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread.sleep alters the output!" Watch "Thread.sleep alters the output!" New topic
Author

Thread.sleep alters the output!

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Hi,

I have got the below piece of code
When it's run it produces output line by line. I wonder why?





www.cs-repository.info
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Works perfectly on my computer. Produces the entire message on just a single line!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What output does it produce?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Read the method you are calling on System.out and see what it does. I shall remind you of the location of its API documentation: here. Look through that API page and you will find other methods; I think one with a rather similar name will help your problem.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(OT: The program as written doesn't produce any output at all for me. Not sure why it would.)
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

David Newton wrote:(OT: The program as written doesn't produce any output at all for me. Not sure why it would.)


Yes right. I think I moved a closing braces (unintentionally).

Even when that mistake is corrected there is no reason why the code should ever produce output on a new line :)
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

David Newton wrote:(OT:


Ehh..what does OT stand for?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Off-Topic
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Have I made a mistake and read println() when he said print()? If so, sorry for confusing everybody.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Hi Sam,

How did you get it working? I tried it separately in a new class and still no use!


Without the sleep method it works fine.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't see how that's possible; are you getting an exception?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Interestingly, John's first code listing, and John's most recent code listing are not the same.

In the first listing, the sysout is in the catch block, and hence, won't print anything unless an exception is thrown. In the most recent listing, it should work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
This is how the output comes:

*
*
*
W
e
l
c
o
m
e

t
o
.
.
. ....

strange
I don't get any exceptions.

Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Henry Wong wrote:
Interestingly, John's first code listing, and John's most recent code listing are not the same.

In the first listing, the sysout is in the catch block, and hence, won't print anything unless an exception is thrown. In the most recent listing, it should work.

Henry


Yep I agree. That's all I did. Move the System.out.print(c); outside the catch. As Henry says this code should work fine.
What happens in your case? No output at all? You do need to wait for a couple of seconds before the output starts showing of course!

P:S -> The code you posted works fine. I did try it out once more with no changes at all
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

john eipe wrote:This is how the output comes:

*
*
*
W
e
l
c
o
m
e

t
o
.
.
. ....

strange
I don't get any exceptions.



Nopes I get a straight line,
***Welcome to the MTH junior CAT***Press any key to continue . . .

Not sure why you are getting it on a different line .. Beats me!!
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
When I ran this from Command Line it worked!
But when I try this in NetBeans I get the previous output. I tried resizing the Output window et all but to no use.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
I ran this in Netbeans with 100 millisec sleep time and it works. But if I increase it, gives the old one.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Yep really strange! Probably something with the ide..Hard to say why though
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread.sleep alters the output!