Multithreading in Java with examples

Multithreading in Java refers to a program's ability to perform multiple tasks concurrently within a single process. This enables a program to take advantage of modern computer architectures, which frequently have multiple processors or cores that can simultaneously work on different tasks.

Threads, which are compact processes that share memory with the main program in Java, are used to implement multithreading. The program can carry out multiple tasks at once because each thread can carry out a distinct task or set of tasks and can run concurrently.

Create a thread in Java

Following are the two ways to create a thread in Java:

Extending the Thread class

You can approach the following general form to create a thread by extending the Thread class in Java:

class MyThread extends Thread {
    public void run() {
        // Code that will run in this thread
    }
}

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

In this syntax or general form, the following code:

class MyThread extends Thread

defines a new class called "MyThread" that extends the "Thread" class. This means that instances of "MyThread" are themselves threads and have access to all the methods and functionality of the "Thread" class. Then, the following statement:

MyThread abc = new MyThread();

creates a new instance of the "MyThread" class, which is a new thread. And, finally, the statement given below:

abc.start();

By calling the start() method on the abc object, a new thread is started. This method triggers the execution of the MyThread class's run() method in the newly created thread.

Implementing the Runnable interface

You can approach the following general form to create a thread by implementing the Runnable interface in Java:

class MyRunnable implements Runnable {
    public void run() {
        // Code that will run in this thread
    }
}

MyRunnable xyz = new MyRunnable();
Thread abc = new Thread(xyz);
abc.start();

In this syntax or general form, the following code:

class MyRunnable implements Runnable

defines a new class called "MyRunnable" that implements the "Runnable" interface. This means that instances of "MyRunnable" can be used to create threads since they implement the "run()" method required by the "Runnable" interface. Then, the following code:

public void run()

defines a run() method for the "MyRunnable" class. The program that will run when the thread is started is contained in this method. Now, the following statement:

MyRunnable xyz = new MyRunnable();

creates a new instance of the "MyRunnable" class, which is a new thread. Then, the following statement:

Thread abc = new Thread(xyz);

creates a new instance of the Thread class with xyz as the Runnable argument. This creates a new thread using the run() method from MyRunnable. And, finally, the statement given below:

abc.start();

starts the new thread by calling the start() method on the abc object. This method causes the run() method of the MyRunnable class to be executed in the new thread.

Java multithreading example

Consider the following program as an example demonstrating multithreading in Java:

Java Code
public class MultithreadingExample {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new MyThread("Hello from thread 1!"));
        Thread thread2 = new Thread(new MyThread("Hello from thread 2!"));
        thread1.start();
        thread2.start();
    }
}

class MyThread implements Runnable {
    private String message;

    public MyThread(String message) {
        this.message = message;
    }

    public void run() {
        System.out.println(message);
    }
}
Output
Hello from thread 2!
Hello from thread 1!

We create two threads in this program by instantiating Thread objects and passing MyThread objects as the Runnable target. MyThread implements the Runnable interface and defines a run method that merely outputs the message to the console.

When we invoke "start" on each thread, they will execute concurrently, as a result, the following output is also generated:

Hello from thread 1!
Hello from thread 2!

Here's the modified code that extends the Thread class instead of implementing the Runnable interface:

public class MultithreadingExample {
    public static void main(String[] args) {
        MyThread thread1 = new MyThread("Hello from thread 1!");
        MyThread thread2 = new MyThread("Hello from thread 2!");
        thread1.start();
        thread2.start();
    }
}

class MyThread extends Thread {
    private String message;

    public MyThread(String message) {
        this.message = message;
    }

    public void run() {
        System.out.println(message);
    }
}

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!