+ 7
How to do delay in java
7 Answers
+ 4
abdul rahman It is cause method sleep can potentially throw an exception in this case you can wrap your code in try/catch
public class Program
{
public static void main(String[] args) {
try {System.out.println("before sleep");
Thread.sleep(1000);
System.out.println("after sleep"); }
catch (Exception e){
System.out.println("Exception");}
}
}
+ 16
You could simply use Thread.sleep(1000) where the argument's in milliseconds. So 1000 means 1 seconds.
Or - for a non-blocking approach - the Timer class:
https://docs.oracle.com/javase/10/docs/api/java/util/Timer.html
+ 10
Thread.sleep(1000)
With argument number of milliseconds
+ 6
I see an error when I write this
+ 4
Thread.sleep(t) as mentioned before should work fine in a try-catch block
+ 1
I did create a delay in c but it was for the detpic32.
+ 1
Used sleep method