Skip to main content

Putting a Java pogram to sleep

For a good night's sleep wear comfortable loose clothes, do not consume coffee or tea after 6:00 PM, and read something relaxing before going to bed. Old hat.

ok that's cool, but what if you want to put your Java Thread to sleep? Before Java SE 5, the most common way to put threads to sleep was by calling the sleep() method.


System.out.println("Sleeping for 150 ms");
Thread.sleep(150);
System.out.println("Sleeping for 2 seconds");
Thread.sleep(1000);
System.out.println("Sleeping for 50 nanoseconds");
Thread.sleep(050);
Java2html

 

 

The sleep() method takes a long parameter that  represents the milliseconds we want the thread to sleep. If the time we want our thread to sleep can be easily represented in ms, then this method works out well, but if we want the thread to sleep for some seconds then we have to perform the conversion manually. Even worse, if we want the thread to sleep for nanoseconds, then we have to use the confusing sleep() method that takes 2 parameters as shown in the code above. Such code is not very readable.

Java SE 5 gives us better readability in the form of the TimeUnit class. TimeUnit is an enum that has defines constants for DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS,  and SECONDS. TimeUnit also has a sleep() method which we can call on the appropriate constant to put the thread to sleep for the number of specified units.


System.out.println("Sleeping for 150 ms");
TimeUnit.MILLISECONDS.sleep(150);
System.out.println("Sleeping for 2 seconds");
TimeUnit.SECONDS.sleep(2);
System.out.println("Sleeping for 50 nanoseconds");
TimeUnit.NANOSECONDS.sleep(50);
Java2html

 

 

Besides this TimeUnit also provides methods for converting values among different time units. Using the TimeUnit enum is definetely preffered over using Thread.sleep() if you are using Java SE 5.

There is one line in the above code samples that I am not sure will work correctly. By "work correctly" I do not mean it will cause a compiler error, but it may not work as promised. Can anyone point out which line?

Discuss this post in the learning forum.



Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

Comments

Popular posts from this blog

My HSQLDB schema inspection story

This is a simple story of my need to inspect the schema of an HSQLDB database for a participar FOREIGN KEY, and the interesting things I had to do to actually inspect it. I am using an HSQLDB 1.8 database in one of my web applications. The application has been developed using the Play framework , which by default uses JPA and Hibernate . A few days back, I wanted to inspect the schema which Hibernate had created for one of my model objects. I started the HSQLDB database on my local machine, and then started the database manager with the following command java -cp ./hsqldb-1.8.0.7.jar org.hsqldb.util.DatabaseManagerSwing When I tried the view the schema of my table, it showed me the columns and column types on that table, but it did not show me columns were FOREIGN KEYs. Image 1: Table schema as shown by HSQLDB's database manager I decided to search on StackOverflow and find out how I could view the full schema of the table in question. I got a few hints, and they all pointed to

Fuctional Programming Principles in Scala - Getting Started

Sometime back I registered for the Functional Programming Principles in Scala , on Coursera. I have been meaning to learn Scala from a while, but have been putting it on the back burner because of other commitments. But  when I saw this course being offered by Martin Odersky, on Coursera , I just had to enroll in it. This course is a 7 week course. I will blog my learning experience and notes here for the next seven weeks (well actually six, since the course started on Sept 18th). The first step was to install the required tools: JDK - Since this is my work machine, I already have a couple of JDK's installed SBT - SBT is the Scala Build Tool. Even though I have not looked into it in detail, it seems like a replacement for Maven. I am sure we will use it for several things, however upto now I only know about two uses for it - to submit assignments (which must be a feature added by the course team), and to start the Scala console. Installed sbt from here , and added the path

Five Reasons Why Your Product Needs an Awesome User Guide

Photo Credit: Peter Merholz ( Creative Commons 2.0 SA License ) A user guide is essentially a book-length document containing instructions for installing, using or troubleshooting a hardware or software product. A user guide can be very brief - for example, only 10 or 20 pages or it can be a full-length book of 200 pages or more. -- prismnet.com As engineers, we give a lot of importance to product design, architecture, code quality, and UX. However, when it comes to the user manual, we often only manage to pay lip service. This is not good. A usable manual is as important as usable software because it is the first line of help for the user and the first line of customer service for the organization. Any organization that prides itself on great customer service must have an awesome user manual for the product. In the spirit of listicles - here are at least five reasons why you should have an awesome user manual! Enhance User Satisfaction In my fourteen years as a