-1

I am wondering if there is a way to do this, without anything confusing or messy. also, when i do a wait() method, it has a java.lang.IllegalMonitorStateException error.

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
mooL
  • 59
  • 1
  • 3
  • 7
  • 1
    `wait` probably doesn't do what you expect. Anyway, you need `Thread.sleep()`. If you don't want to mess with that try-catch stuff, just put it in a utility-method and call it `saveSleep` or something – tkausl Jun 24 '16 at 04:00
  • Would you find nay luck [here](http://stackoverflow.com/questions/1519091/scheduledexecutorservice-with-variable-delay) – Aown Raza Jun 24 '16 at 04:01
  • Search Stack Overflow thoroughly before posting. – Basil Bourque Oct 14 '18 at 23:12

4 Answers4

4

The Thread.sleep() method can do what you want. It's a simple approach that stops execution for a given amount of time (not always accurate). Per the Oracle docs:

Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

So, to call it, use,

Thread.sleep(1000);

This will sleep for one second until further execution. The time is in milliseconds or nanoseconds.

This method may not always be accurate due to the OS and its configuration.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
2

Once again, Guava is your friend:

Uninterruptibles.sleepUninterruptibly(1,TimeUnit.SECONDS);

and this is how it is implemented:

 public static void sleepUninterruptibly(long sleepFor, TimeUnit unit) {
    boolean interrupted = false;
    try {
      long remainingNanos = unit.toNanos(sleepFor);
      long end = System.nanoTime() + remainingNanos;
      while (true) {
        try {
          // TimeUnit.sleep() treats negative timeouts just like zero.
          NANOSECONDS.sleep(remainingNanos);
          return;
        } catch (InterruptedException e) {
          interrupted = true;
          remainingNanos = end - System.nanoTime();
        }
      }
    } finally {
      if (interrupted) {
        Thread.currentThread().interrupt();
      }
    }
  }

br

Moonlit
  • 5,171
  • 14
  • 57
  • 95
1

In Java

Thread.sleep(intervalInMills);
TimeUnit.MILLISECONDS.sleep(intervalInMills);

Timer

new Timer().scheduleAtFixedRate(task, delay, period);

With Executor Framework

ScheduledExecutorService.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

With Spring,

@Scheduled(fixedDelay = 1000)
private void method() {
    // some code
}

You can also schedule cron or fixedRate with initialDelay.

Saravana
  • 12,647
  • 2
  • 39
  • 57
0

Without Thread and try/catch:

static void pause(){
    long Time0 = System.currentTimeMillis();
    long Time1;
    long runTime = 0;
    while (runTime < 1000) { // 1000 milliseconds or 1 second
        Time1 = System.currentTimeMillis();
        runTime = Time1 - Time0;
    }
}

Update:

awilkinson is right, the above method is a really bad hack. By the way, if you just want a ready to use method to wait for a second or more, even if it uses try/catch, then I would recommend this:

public static void pause(double seconds)
{
    try {
        Thread.sleep((long) (seconds * 1000));
    } catch (InterruptedException e) {}
}
M Imam Pratama
  • 998
  • 11
  • 26
  • 1
    This approach should not be used because it will continually execute the loop using CPU cycles for the full wait time. – awilkinson Aug 07 '18 at 21:23