This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sleep() method?

 
Asleshkumar Thukaram
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just now learning the concepts of threads.....I tried to execute this pgm :
class td{
public static void main(String args[]){
Thread t=Thread.currentThread();
System.out.println("current thr " +t);
System.out.println("after name change ");
try{
for(int i=5;i>5;i--){
System.out.println(i);
t.sleep(1000);
}
}catch (InterruptedException e){
System.out.println("main thread interrupted");
}
}
}
But as expected, the pgm had to print the numbers 1,2,3,4 and 5, in an interval of 1 sec between each number, but it didn't print. The output just stopped with the print of "after name change ". Why doesn't that print the numbers??
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

for(int i=5;i>5;i--){
System.out.println(i);
t.sleep(1000);
}

Hi,
loop is not coded as expected. you can code it like "for(int i=5;i<=1;i--)".
hope it helps
regds
George
George, We all make mistakes. It will be helpful if we help others rectify their mistakes. I have modified your message to remove inappropriate content.

[This message has been edited by Rahul Mahindrakar (edited January 28, 2001).]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

loop is not coded as expected. you can code it like "for(int i=5;i<=1;i--)". <br />
<br />
might work better.
- Peter
Peter , I have removed content that i feel is inappropriate.

[This message has been edited by Rahul Mahindrakar (edited January 28, 2001).]
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I am sorry, it is a type error. Thank you for correcting it.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!