Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Delay Example

 
 

Share on Google+Share on Google+

Delay Example

Advertisement
This section learns you to make delay between the execution of program.

Delay Example

     

In this section we learn how we can make a program so that there are delay between the execution of program.

In this example we are using a sleep() method. We are passing 4000 to give the waiting time to our thread. This mean at execution time our program will wait for 4000 millisecond. The sleep() method can throws an InterruptedException .Show we must have write the sleep() method within try/catch block.  

 

 

 

 

The code of the program is given below:

public class DelayExample{
  public static void main(String[] args){
  System.out.println("Hi");
  for (int i = 0; i < 10; i++)
  {
  System.out.println("Number of itartion = " + i);
  System.out.println("Wait:");
  try
  {
  Thread.sleep(4000);  
 
  }catch (InterruptedException ie)
  {
  System.out.println(ie.getMessage());
  }
  }
  }
}

The output of the program is given below:

C:\convert\rajesh\completed>javac DelayExample.java
C:\convert\rajesh\completed>java DelayExample
Hi
Number of itartion = 0
Wait:
Number of itartion = 1
Wait:
Number of itartion = 2
Wait:
Number of itartion = 3
Wait:
Number of itartion = 4
Wait:
Number of itartion = 5
Wait:
Number of itartion = 6
Wait:
Number of itartion = 7
Wait:
Number of itartion = 8
Wait:
Number of itartion = 9
-Wait:

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 29, 2007

Related Tutorials

Discuss: Delay Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
Ankur Patro
March 4, 2012
Program using system clock

To write a program in order to print a set of integers in randomized order or shuffled way widout repeatation of a particular integer.
niranjan
April 18, 2012
search for delay

nice site
Ruben
May 2, 2012
Delay Help

Thank You So much, this example really helped a lot. I Really apprechiate it:)
zia jappa
May 13, 2012
Thanx

Thaan you. This is very helpful site... go on like this.
DMCA.com