Search code examples
javadelaywait

Wait without try-catch


I am working on a Java program that runs forever, but I don't know how to delay each loop for one second without using 'try catch'. I've tried

import java.util.concurrent.TimeUnit;

public class program {
    public static void main(String[] args) {
        TimeUnit wait = new TimeUnit();
        while (true) {
            wait.SECONDS.sleep(1);
        }
    }
}

but it didn't seem to work. Can anyone help me?


Solution

  • Someone said that I should do this

    import java.util.concurrent.TimeUnit;
    
    public class program {
        public static void main(String[] args) throws InterruptedException {
            while (true) {
                TimeUnit.SECONDS.sleep(1);
            }
        }
    }
    

    and it worked. Thank you that person