Bai Radule Bai Radule - 7 months ago 12
Java Question

How to have a break of a few seconds in execution of program

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?

Answer

Just add the Thread.sleep().

for (...) {
    //print the element
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
        //do things with exception
    }
}