This question might sound strange but I'm interested to know if somehow i can have a break of a few seconds in execution of a program.For example when you have a simple for() for printing out the elemnts of an array,the elements will be printed all directcly.I was wondering if it is possible to print like the first element then after a break of 2 second print the second one and so on until the last one.Is something like this possible?
Asked
Active
Viewed 1,817 times
0
-
1You should check out http://stackoverflow.com/questions/3342651/how-can-i-delay-a-java-program-for-a-few-seconds – Dorin Rusu Apr 23 '16 at 10:32
-
Thread.sleep(2000); ? https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html – Devrim Apr 23 '16 at 10:32
-
1You could use Thread.currentThread().sleep() but this is not guaranteed to sleep for exactly 2 seconds. Could be more. Or you could use a while loop that subtracts from the current time the time at which the last value was printed and see if the difference is 2 seconds and then print the next value. This is a busy wait while the other option with sleep() leaves your processor free to do other tasks. – MS Srikkanth Apr 23 '16 at 10:32
-
However, Java's time resolution is about (literally) a thousand times better than C++, and on a normal PC lies somewhere in the vicinity of 50 nanoseconds. – JayC667 Apr 23 '16 at 10:36
-
Didn't wait it to seem like self-promotion, so I linked the duplicate to the earlier question. But I would recommend [my approach](http://stackoverflow.com/a/24104427/2071828). – Boris the Spider Apr 23 '16 at 10:36
-
1@JayC667 - That doesn't change the fact that sleep() depends on the underlying OS's scheduler which doesn't have to be as accurate as java expects it to be. – MS Srikkanth Apr 23 '16 at 10:38
-
@BoristheSpider - From the OP's exercise, it seems a ScheduledExecutorService seems to be too much of an ask though that would be the right way to go if your task is more meaningful than printing a single number. – MS Srikkanth Apr 23 '16 at 10:39
-
@user3493289 given the OP wants to wait for two **seconds**, waiting for a few extra **microseconds** is unlikely to make a different. Checking the current time would not solve this, as the resolution of the system clock is no better. `System.nanoTime` would be the only way to make your solution more accurate. – Boris the Spider Apr 23 '16 at 10:40
-
@user3493289 drift likely isn't a problem in this case. I was more referring to `TimeUnit.sleep` rather than `Thread.sleep`. – Boris the Spider Apr 23 '16 at 10:40
-
@BoristheSpider - I didnt disagree with you. Just said that ExecutorService would be an overkill for this exercise. Anyway TimeUnit.sleep internally calls Thread.sleep(). All that I said was that sleep() would be a better solution as the accuracy of 2 seconds is not important in this case and is a better solution than subtracting from the current time as it avoids busy waiting. – MS Srikkanth Apr 23 '16 at 10:43
5 Answers
2
Just add the Thread.sleep().
for (...) {
//print the element
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
//do things with exception
}
}

Mike B
- 2,756
- 2
- 16
- 28
0
You can use Thread.sleep(1000)
method in for loop:
public class JavaApp{
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
It prints from 0 to 9, one number per second.
run:
0
1
2
3
4
5
6
7
8
9
**BUILD SUCCESSFUL (total time: 10 seconds)**

EbraHim
- 2,279
- 2
- 16
- 28
0
I recommend to use Thread.sleep()
Try this :
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
In this way, the program will pause for 1000 milliseconds.

Jad Chahine
- 6,849
- 8
- 37
- 59
-
-
@EbraHim : This exception is thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. – Jad Chahine Apr 23 '16 at 10:41
0
Thread.currentThread.sleep(time in ms);

Tiago Dall'Oca
- 329
- 3
- 15
-
1Please add some explanaition of the code or at least a link to the programming reference or guide. – try-catch-finally Apr 23 '16 at 12:07