-->
Skip to main content

What is Delay Loop in Java?

What is Delay Loop in Java?

In this tutorial, we will cover What is Delay loop in java.

What is Delay Loop in Java

Delay Loop in Java

The word Delay Loop simply refers to a small java program that has an inbuilt delay in execution for certain purposes.

For Example:

In real-time applications, if we are using third-party Rest Api which runs synchronously so we put some delay between two API calls so that the first API hit response comes back within that time.

Code in Action: 

 public class DelayLoopExample extends Thread {  
   @Override 
   public void run() { 
     for (int i = 1; i <= 10; i++) { 
      System.out.println("Element : "+ i + " is in process : " + LocalTime.now()); 
      try { 
       Thread.sleep(3000); 
      } catch (InterruptedException e) { 
      e.printStackTrace(); 
      } 
     }  
   }
	 
   public static void main(String[] args) {		 
     DelayLoopExample example = new DelayLoopExample(); 
     example.start();		 
   } 
 } 

Output:

Element : 1 is in process : 19:47:56.341
Element : 2 is in process : 19:47:59.354
Element : 3 is in process : 19:48:02.360
Element : 4 is in process : 19:48:05.382
Element : 5 is in process : 19:48:08.398
Element : 6 is in process : 19:48:11.400
Element : 7 is in process : 19:48:14.410
Element : 8 is in process : 19:48:17.416
Element : 9 is in process : 19:48:20.430
Element : 10 is in process : 19:48:23.441

In the above example, we have halted the execution of the loop for 3 seconds. So in between each line that prints will have 3 seconds gap as we can see in the time printed in the output.

Conclusion:

In this tutorial, we have covered What is Delay loop in Java.

Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

So what do you think about this tutorial? It's Easy Right?

Happy Learning!!!

More Examples you may like

Comments