Close

Creating Threads in Java

[Last Updated: Mar 22, 2016]

Please see a quick general description of threads and concurrency if you are not already familiar with them.

In Java there are two ways to create thread.

  • By extending java.lang.Thread class.
  • By implementing java.lang.Runnable interface.

JDK 1.5 introduced Concurrency API in the package java.util.concurrent which has Utility classes commonly useful in creating and managing threads. In this tutorial, however, we are going to give example of above two basic method of creating threads.



Create thread by extending Thread

public class ThreadExample {
    public static void main (String[] args) {
        MyThread thread = new MyThread();
        thread.start();

        MyThread thread2 = new MyThread();
        thread2.start();

        System.out.println(Thread.currentThread()
                                 .getName());
    }

    private static class MyThread extends Thread {
        @Override
        public void run () {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread()
                                         .getName() + ": " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Output:

main
Thread-1: 0
Thread-0: 0
Thread-1: 1
Thread-0: 1
Thread-1: 2
Thread-0: 2
Thread-1: 3
Thread-0: 3
Thread-0: 4
Thread-1: 4



Creating Thread by implementing Runnable

public class ThreadRunnableExample {

    public static void main (String[] args) {
        MyThreadRunner threadRunnable = new MyThreadRunner();
        Thread thread = new Thread(threadRunnable);
        thread.start();

        MyThreadRunner threadRunnable2 = new MyThreadRunner();
        Thread thread2 = new Thread(threadRunnable2);
        thread2.start();

        System.out.println(Thread.currentThread()
                                 .getName());
    }

    private static class MyThreadRunner implements Runnable {
        @Override
        public void run () {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread()
                                         .getName() + ": " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Output:

main
Thread-0: 0
Thread-1: 0
Thread-0: 1
Thread-1: 1
Thread-1: 2
Thread-0: 2
Thread-1: 3
Thread-0: 3
Thread-0: 4
Thread-1: 4


The method Thread#start()

In both above methods of creating threads, the call Thread#start() creates a new thread and the corresponding Runnable#run method is invoked in that thread. That means Thread#start() returns asynchronously and the Runnable#run method is called back in the new thread.

The method Thread#start() can be called only once. That means a Thread object cannot be used multiple times.



The static method Thread#currentThread(..)

This method returns the reference of the currently executing thread. 'Currently' refers to the point where this method is called, meaning Thread#currentThread returns the instance of that thread in which this method is called.



Thread#sleep()

This method causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.



Thread Name

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it. User may set the name of thread by calling Thread#setName(...)

Example Project

Dependencies and Technologies Used:

  • JDK 1.8
  • Maven 3.0.4

Java Creating Thread Example Select All Download
  • java-create-threads
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ThreadExample.java

    See Also