Java supports concurrent programming, with its support for Thread programming. A Thread in Java is a small part of the program which, the compiler executes individually. In other words, threads are light-weight processes within a process. It is a sequential path of execution.

In Java, a Thread is represented by the java.lang.Thread class. The JVM treats a thread as an object of the java.lang.Thread class whose behaviour is defined in the run method, thus in Java, a thread is an object of the Thread class.

The behaviour of the thread is the run() , which is actually declared in an interface called a runnable interface.

Advantages of Thread:

  • A Thread is a lightweight process
  • Thus, we can do one or more tasks simultaneously. For Example, In the word document spell checker thread running internally, while we are typing.
  • Inter-thread communication is less expensive than inter-process communication.
Thread

How to Create A Thread In Java?

To create a thread in Java, we can follow any one of the below approaches:

  • The Extinction mechanism
  • The Implementation mechanism

The Extension Mechanism:

In the Extension mechanism, we can create a subclass of our own, that extends the Thread class and define the Thread behaviour within the newly created class. And whenever required, we can instantiate the subclass and start the Thread behaviour with the start() method.

Example:

class MyThread  extends Thread {
    public void run() {
        -----
        -----
    }
}
Thread t = new MyThread();
t.start();

The Implementation Mechanism:

In the implementation mechanism, we can create a class which implements the runnable interface. The runnable interface is the interface which contains the declaration of the Thread behaviour run().

Example:

class MyThread  implements Runnable {
    public void run() {
        -----
        -----
    }
}

Actually, the above class MyThread is not a thread, because we can not acquire all the Thread class properties here. We acquire only the Thread behaviour called run(). Further, in this case, we don’t call the run method with the start() method, because start() is not available in the Runnable interface. Because it is actually coming from the Thread class. So, we don’t call it a Thread. If we want to make the class as a Thread we need to follow the below syntax.

Syntax:

Thread t = new Thread(new MyThread());
t.start();

The above statement makes the MyThread class as a Thread object. And t.start() executes the behaviour as defined in the MyThread class.

The Implementation Mechanism is usually better than the Extension Mechanism, as it allows us to separate the Thread behaviour definition from the object itself and thereby giving us enhanced flexibility.

Possible constructors:

Thread() – It allocates a new Thread object.

Thread(String name) – Thread Object which will associate with the name.

Thread(Runnable target) – It allocates a new Thread object.

Thread(Runnable target,String name) – Creates a Thread object with the given name.

Thread(ThreadGroup group,String name) – We can create a thread group by using this constructor.

From the JVM’s point of view, each program will internally be converted as a Thread, from which all the child threads are created. Those may be Daemon Threads or Non-Daemon Threads. By default, all threads are Non-Deamon threads.

A Daemon Thread is a low priority thread (in JVM’s point of view ) that runs in the background to perform tasks such as garbage collection, etc.,

A Non-Daemon Thread is any thread, which the main thread creates. It runs in the main method in Java is by default non-daemon. Because the thread inherits its daemon nature from its parent thread. Since the main thread is a non-daemon thread, any other thread created from it will remain non-daemon. We can make it a daemon thread explicitly by calling setDaemon(true).

By default, every thread is given a name and a priority.

Example :

MainThread.java
public class MainThread {
    public static void main(String[] args) {
        Thread t = Thread.currentThread();
        System.out.println("Thread Name : " + t.getName());
        System.out.println("Thread Priority : " + t.getPriority());
        System.out.println("Thread Group : " + t.getThreadGroup().getName());
        System.out.println("Thread State : " + t.getState());
    }
}

Output:

Thread Name : main
Thread Priority : 5
Thread Group : main
Thread State : RUNNABLE

In the above example, the MainThread class didn’t extend the Thread class and not even implement the runnable interface. But the output tells us the name, priority, group and state of the thread.

Happy Learning 🙂

About the Author:

Avatar
Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment