?
Solved

thread sleep program

Posted on 2011-04-26
4
Medium Priority
?
388 Views
Last Modified: 2012-05-11
>>public class TSleep extends Thread{
public static void main(String argv[]){
       TSleep t = new TSleep();
       t.start();
       }

       public void run(){
          try{
             while(true){
                  this.sleep(10);
                  System.out.println("looping while");
                 }
            }catch(InterruptedException ie){}
       }
}
 
   

I was running above program to see thread sleep after 10 milli second. But it never slept. It is going continuously in infinity loop. please advise.



Any ideas, resources, links, sample code highly appreciated. thanks in advance.
0
Comment
Question by:gudii9
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35471406

But when you say while(true) {...}
and inside your loop there is no break;
so it will never get out of that loop,
because true is alwyas true

You should have something like that:


  int count =0;
      while(true){
                  this.sleep(10);
                  System.out.println("looping while");
                 count++;
         if (count > 10) break;
                 }

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35471415
in general to see how program sleeps
you don't need to create separate thread

you can say in any place of your code:

Thread.currentThread().sleep(1000);

this will sleep for a second
0
 
LVL 7

Author Comment

by:gudii9
ID: 35471424
>>>

What will happen when you attempt to compile and run this code?

public class TGo implements Runnable{
public static void main(String argv[]){
        TGo tg = new TGo();
        Thread t = new Thread(tg);
        t.start();
        }
        public void run(){
                while(true){
                        Thread.currentThread().sleep(1000);
                        System.out.println("looping while");
                        }
        }
}
1) Compilation and no output
2) Compilation and repeated output of "looping while"
3) Compilation and single output of "looping while"
4) Compile time error



similar lines this program wont give the exception also. please advise
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35471445
This will give compile error because line

Thread.currentThread().sleep(1000);


requires InterruptedException to be caught
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question