+ 7

How to do delay in java

2nd May 2019, 5:42 AM
Abdul rahman;
Abdul rahman; - avatar
7 Antworten
+ 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");} } }
2nd May 2019, 7:17 AM
Dima Makieiev
Dima Makieiev - avatar
+ 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
2nd May 2019, 5:47 AM
Tashi N
Tashi N - avatar
+ 10
Thread.sleep(1000) With argument number of milliseconds
2nd May 2019, 5:45 AM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 6
I see an error when I write this
2nd May 2019, 5:47 AM
Abdul rahman;
Abdul rahman; - avatar
+ 4
Thread.sleep(t) as mentioned before should work fine in a try-catch block
2nd May 2019, 5:14 PM
Mau
Mau - avatar
+ 1
I did create a delay in c but it was for the detpic32.
2nd May 2019, 1:33 PM
Werg Serium
Werg Serium - avatar
+ 1
Used sleep method
2nd May 2019, 2:07 PM
Rahman Rezaie 🇦🇫
Rahman Rezaie 🇦🇫 - avatar