How to create thread in Java

Ways to create thread in Java

In this tutorial, we are going to see how to create a thread in Java. There are two ways to create threads in Java.

1. Using Thread Class

2. Using Runnable interface

How to create thread in Java

Create a Thread in Java using Thread Class

We can create using Thread class. Java provides thread class to create threads by extending this class. Please see below code snippet.

package com.slb.thread;

public class ThreadDemoUsingExtend  extends  Thread{

    @Override
    public void run() {
        System.out.println("Running Thread....");
    }
}

Here, in the above code snippet, you can see we are extending a class called ‘Thread’ and overriding the run method which will execute once we call the start() method on that thread.

 public static void main(String[] args) {
        ThreadDemoUsingExtend threadObject = new ThreadDemoUsingExtend();
        threadObject.start();
    }

Here, we have created the thread class object named “threadObject” inside main method and calling the start() method on it, which will call the run() method.

Output:

Running Thread....
Process finished with exit code 0

Note that, the start() method call (threadObject.start()) returns as soon as we call it, it will not wait until the run() method is done.

Using Runnable interface

There is another way to create threads in Java using the java.lang.runnable interface provided by Java. The runnable interface has only one method, i.e. run(). Look at the interface below.

package java.lang;
//before java 8
public interface Runnable {
    void run();
}

//After java 8 it is functional interface as

package java.lang;

@FunctionalInterface
public interface Runnable {
    void run();
}

We will create a thread and implement this interface as shown below:

package com.slb.thread;

public class ThreadB implements Runnable{
    @Override
    public void run() {
        System.out.println("in run() of ThreadB class which implements Runnable interface");
    }
}

In the above code snippet, we have created one class and implemented the runnable interface, which has run() method which will execute the code inside it.

There is another way to implement by creating anonymous class.

public class ThreadDemo {

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("running...");
            }
        };
        runnable.run();
    }
}

Creating Thread using Java 8

From Java 8 and above, you can also use lambda to create a runnable implementation. Runnable is a functional interface in java. Please see below example of using lambda expression that implements runnable :

public class ThreadJava8 {

    public static void main(String[] args) {

        Runnable runnable = () -> {
            System.out.println("inside run method of lambda expression");
        };
    }
}

In the above code snippet, you can see we are using a lambda expression which automatically provides implementation of run method inside its expression.

Types of Threads in java

There are two types of threads in java

  1. User Threads 2. System Thread

User threads are normal threads created by users, and system threads are daemon threads created by system or even user can make any thread as daemon.

Checkout to know more about Daemon Threads | Checkout Multithreading here

Checkout the complete code used here at SLB-GitHub.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *