3

How can I make a java program wait X seconds without usingThread.sleep?

public class newClass{
     public static void main(String[] args){
           for(int i = 0; i < 10; i++){
               //Wait X second 
               System.out.println("Hello");
           }
     }
}

Thanks for any help!!

I need the program won't freeze while it's counting

15
  • wait() to be notified by whom? Commented Aug 6, 2014 at 12:57
  • 2
    "Make program wait" is exactly what sleep does. So what is your issue? Commented Aug 6, 2014 at 12:57
  • 1
    Are you asking about busy waiting? Commented Aug 6, 2014 at 12:58
  • 1
    What do you reckon the difference between waiting and sleeping is? Commented Aug 6, 2014 at 12:59
  • What is the supposed difference between wait/sleep? Commented Aug 6, 2014 at 12:59

5 Answers 5

1

You can use Java's Timer and TimerTask class for this.

 import java.util.Timer;
 import java.util.TimerTask;
 public class newClass extends TimerTask{
      int i = 10;
      static Timer timer;

      @Override
      public void run(){
           System.out.println("hello");
           i--;
           if (i == 0)
               timer.cancel();

      }

      public static void main(String[] args){
           TimerTask timerTask = new newClass();
           timer = new Timer(false);
           timer.scheduleAtFixedRate(timerTask, 0, 5 * 1000);
      }
 }

And Timer works on its own thread in the background so it won't freeze up the rest of your program!

Sign up to request clarification or add additional context in comments.

13 Comments

How can i make it write 10 times "Hello"? Can you change the code that it will do that?
The TimerTask should repeat at a fixed interval until you call cancel(), so this code should repeat 10 times. Is it not working that way?
No i don't know why because i never used code like this
Ok, just change the line "timer = new Timer(true)" to "timer = new Timer(false)" and tell me if that works
Yes it works !! Can you explain the code to me? With comments it also good :)
|
1

If your issue is that you do not want your program to freeze, consider using threads. This example might be similar to what you want to achieve: http://docs.oracle.com/javase/tutorial/essential/concurrency/simple.html.

Comments

0

It is a bad approach but you can use this:

 public class newClass{
      public static void main(String[] args){
           int X = 5;
           for(int i = 0; i < 10; i++){
               long s = System.nanoTime();
               while( (System.nanoTime() - s) / 1000000000 < X);
               System.out.println("Hello");
           }
      }
 }

You can also use

System.currentTimeMillis()

7 Comments

This what i mean ! Can you explain it to me?
In each while loop step, this code controls current time and calculates the elapsed time in seconds. It is bad because it will use CPU cycles, unlike sleep() or wait() commands, so this is only a bad trick.
No wait it still make my software freeze :(
How can i use a wait()?
If you don't want your program to freeze, you should use a multithread approach like ian mentioned.
|
0

What you are really looking for is a multi-thread implementation. That is what will allow you to continue interacting with one area of the application while waiting for another to finish processing.

Edit Ok, after further clarification in the comments, sounds like this is not what you are looking for. Perhaps you can register each child in a HashMap with the time that they started. Then monitor the map to see when time is up.

Comments

0

Seems like you want your program to start, and after X time has elapsed print "Hello" on the output stream?

  1. Do as ocozalp suggested
  2. Or use the Thread.sleep(x); method

Both options will give you exactly the same result, although option 2 is preferred, since you allow the CPU to do something else while your main thread is sleeping, and because using option 1 makes your intention with the program less clear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.