You can just call us +4 143 508 0794


home / blog / How to make Java Wait

Agile Framework – Testing and Usability

March 19, 2012

Immediate disaster recovery assistance to SME

March 27, 2012

How to make Java Wait

March 23, 2012

Hello! I’d like to show you couple ways how to make Java Wait.

For somebody who doesn’t know or forgot what Java means I will remind.

A little bit from the history. Java is a high-level programming language originally developed by James Gosling(Sun Microsystems). This programming language was designed for set-top boxes and hand held devices. Firstly this language was called OAK but in 1995 Sun Microsystems decided to change the name to Java.

I hope this information wasn’t something new because this topic doesn’t cover general things about Java. Here we want to talk about a really simple but frequently asked question – “How to make Java wait?”.

Sometimes we want to make Java wait before printing something in the terminal window. There are couple ways to solve this problem and it depends on your needs.

First method:

If you want to make Java wait during k-seconds you have to write this code:

public static void wait (int k){
long time0, time1;
time0 = System.currentTimeMillis();
time1 = System.currentTimeMillis();
while (time1 – time0) < k * 1000);

Second method:

Using the Thread.sleep() method you can block the current thread for a given period of time. See sample below.

// how to make Java wait during 5 seconds
// how to make Java wait during 3.5 seconds
Thread.sleep(3000L, 50000L);

I hope the article made clear, how you can block the thread during a given period of time. See you next friday.