IT Blog, Java Development,

How to make Java Wait

1 m read
QArea Expert
QArea Expert Marketing Manager
March 23, 2012
Share on
Reading Time: 1 minute

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();
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.

Categories

Recent Posts

  • Refactor or not refactor? Everything there is to know about refactoring

    Read more
  • What is Ambient Proximity? Definetely More Than A Buzzword

    Read more
  • GDPR: why so serious?

    Read more
Yes
Share on
Privacy Preference Center
Your Privacy

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.

Strictly Necessary Cookies Always Active

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.

You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.

Performance Cookies

These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site.

All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.

Cookies used

Google Analytics
Functional Cookies

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.

Cookies used

Test Second Test
Targeting Cookies

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.

More Information

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.