Intro-to-Java-Programming

How to make Java Wait

Hello! This is Friday’s Answers and today 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();
do{
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
Thread.sleep(5000L);
// 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.


QArea is ready to provide your project with experienced dedicated team. They are highly-skilled in creation of any multifunctional application or cross platform on Java. If you are seeking for competitive solution to be applied for your business, you can get free assistance.


VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

    Facebook Comments

    comments

    'How to make Java Wait' have no comments

    Be the first to comment this post!

    Would you like to share your thoughts?

    Your email address will not be published.

    Software development and outsourcing blog by QArea © 2016

    Яндекс.Метрика