• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.sleep alters the output!

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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




 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works perfectly on my computer. Produces the entire message on just a single line!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What output does it produce?
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(OT: The program as written doesn't produce any output at all for me. Not sure why it would.)
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:(OT:


Ehh..what does OT stand for?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off-Topic
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have I made a mistake and read println() when he said print()? If so, sorry for confusing everybody.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how that's possible; are you getting an exception?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep really strange! Probably something with the ide..Hard to say why though
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic