How to make Java Wait
IT Blog, Java Development,

How to make Java Wait

1 m read
QArea Expert
QArea Expert
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

  • Go Community: The Best Golang conferences of 2018 (and a few to look out for in 2019)

    Read more
  • From Gaming Videos to Redefining Instant Messaging: Quinn Hu's Long Path to Serial Entrepreneurship

    Read more
  • 7 Reasons to Truly Love Microservices

    Read more
  • The Best Languages for Microservices

    Read more
  • QArea's Year: Summing Up 2018

    Read more
  • What's New in Golang 1.11: Release Notes Overview

    Read more
  • Why You Should Start Learning Dart and Flutter Right Now

    Read more
  • Golang Vs Python: Which Language Is Best for AI Programming

    Read more

Subscribe

Yes

Share on
Close icon
Privacy Preference Center