Java

Java Thread Sleep

Java Thread Sleep

A thread can be made to halt its execution for some time before it continues to operate. The thread is considered to sleep, in the period that it halts. But, what is a thread? A thread is a sub-program. It is possible to have more than one thread in one program, running concurrently. The main program itself is the main thread. A single threaded program is just the normal program. In Java, the normal program is the class that has the main() method. In a multi-threaded program, the main() method, can be considered as the main thread.

A thread can be created by sub-classing the Thread class. The thread can be made to sleep. This article explains how to make a thread sleep, beginning with a summary of how to create a new thread in Java. The Thread class does not need to be imported in order to have a thread in the program.

Article Content

Summary for Creating a Thread

A class for creating from the Thread class, can be:

class Thr extends Thread {
        void mthd() {
            System.out.println("a method");
        }  
     }

The class thread name is Thr. It extends Thread. It has just one method as a member. The main thread of the main class can be:

    public class TheClass {
        public static void main(String[] args) {
            Thr thr1  = new Thr();
            thr1.start();
            System.out.println("Some main method statements");
            thr1.mthd();
        }
    }

The thread object is thr1, instantiated from Thr. After creating the thread object, the thread (object) must be started, in order to run, with the expression:

            thr1.start();

Just after this point, there are two threads running: the main thread, and thr1. From this point in the thread (main method), where the new thread object has been created, the fields and methods of the new thread object, can be called. The output of the above program is:

Some main method statements
a method

Sleep Syntaxes

sleep() syntaxes are:

    public static void sleep(long millis) throws InterruptedException

and

    public static void sleep(long millis, int nanos) throws InterruptedException

Do not worry about the complexities of the syntaxes; they are easy to use. Each can be used in a thread method. The calling thread in this article, is the main thread (main method).

The first syntax has only one parameter. This is for the duration that the thread has to sleep in milliseconds. If milliseconds is not fine-tuned enough for the programmer, then the programmer can use the second syntax, wherein the first parameter is the number of milliseconds, and the second parameter is the number of nano seconds for fine-tuning (smaller time added measurement).

Sleeping from within Thread

In the syntax, sleep() is a static method. This means that in order to use it, the class name should be employed, instead of the object name. The expression, “throws InterruptedException” is typed on the right of the method signature of interest and not alongside the sleep() method invocation. The above class, Thr is modified as follows:

     class Thr extends Thread {
        void mthd() throws InterruptedException {
            System.out.println("a method");
            Thr.sleep(1000);
            System.out.println("a method");
        }  
     }

The method, mthd() will print “a method”, sleep (wait) for 1 second (1000 milliseconds), and then print “a method” again. In the main() method, the code is:

    public class TheClass {
        public static void main(String[] args) {
            Thr thr1  = new Thr();
            thr1.start();
            System.out.println("Some main method statements");
            try {
                thr1.mthd();
            }
            catch (InterruptedException e) {
                System.out.println("Exception message: " + e.getMessage());
            }
        }
    }

The invocation, “thr1.mthd();” is now in the try-block. There is the corresponding catch-block. The output is:

Some main method statements
a method

and after 1 second, the following line is displayed:

a method

Two Argument sleep() Method

The second syntax is a static method. This means that in order to use it, the class name should be employed. The expression, “throws InterruptedException” is typed on the right of the method signature of interest and not alongside the sleep() method invocation. So, the above class, Thr is modified as follows:

     class Thr extends Thread {
        void mthd() throws InterruptedException {
            System.out.println("a method");
            Thr.sleep(1000, 1000);
            System.out.println("a method");
        }
     }

The method, mthd() will print “a method”, sleep for 1 second and 1000 nanoseconds, and then print “a method” again. In the main() method, the code is:

    public class TheClass {
        public static void main(String[] args) {
            Thr thr1  = new Thr();
            thr1.start();
            System.out.println("Some main method statements");
            try {
                thr1.mthd();
            }
            catch (InterruptedException e) {
                System.out.println("Exception message: " + e.getMessage());
            }
        }
    }

The invocation, “thr1.mthd();” is now in the try-block. There is the corresponding catch-block. The output is:

Some main method statements
a method

and after 1 second and 1000ns, the following line is displayed:

a method

Conclusion

A thread can be made to halt its execution for some time before it continues to operate. The thread is considered to sleep in the period that it halts. But, what is a thread? A thread is a sub-program. It is possible to have more than one thread, in one program, running concurrently. The main program itself, is the main thread. A single threaded program is just the normal program. In Java, the normal program is the class that has the main() method. In a multi-threaded program, the main() method, can be considered as the main thread.

A thread can be created by sub-classing the Thread class. Sub-classing means inheriting from the thread class. The thread can be made to sleep. Sleeping means that the thread stops operating for some time. When a thread is executing, it is largely the methods of the object (class) that are running, and not really the fields. In Java class, property is called field. So, the sleeping code has to be placed in a method. Since the sleep() methods involve an exception, the try-catch compound statement has to be used in the calling thread. The Thread class does not need to be imported, in order to have a thread in the program.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.