How to create threads in java - 3 Ways to achieve multithreading in java

Java is a powerful programming language, which supports multithreading. 

Before going forward let's understand what a thread is and what is multithreading.


Thread

Thread is an independent path of execution within a process, it is considered as a lightweight process. Every program has at least one thread. 
The main benefit of thread is that it is memory efficient and consumes less memory than process because it uses shared memory.
It also helps us to make our program fast and utilize CPU efficiently.

Process vs thread, Multithreading


Multithreading

Multithreading is technique to run program on multiple threads independently. It is used to achieve multitasking. 
In multithreading threads does not block execution of each other and also do not block the user interaction, so it is safe to implement multithreading.

multithreading is used for concurrent execution and parallelism.

Multithreading saves the time of execution of process because each thread run independently and concurrently.

How to create thread in java

There are mainly 3 ways to create threads in java.
  • By Extending Thread Class
  • By Implementing Runnable Interface
  • By creating object of Thread Class using lambda


Extending Thread Class

Thread is a class of java.lang package, as we know this is default package in java, so we can use it without importing it.

Thread class has a run method, we need to override this function in order to execute our code in new thread.
The run method will be called by JVM when thread is started.
To start a thread, we can call start() method on the object of the Thread.

Code: 
public class MultiThreading{
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        
        // Start the thread
        myThread.start();
    }
}

class MyThread extends Thread{
    @Override
    public void run() {
        System.err.println("Current Thread Name: " + Thread.currentThread().getName());
    }
}

Output:
Current Thread Name: Thread-0

Implementing Runnable Interface

Runnable is an interface in java.lang package, to use this we can implement this interface in a class.
The Thread class accepts Runnable interface in constructor, so in order to create a thread we need to create an object of class which implements Runnable interface, then create an object of Thread class and pass the created object of Runnable in the constructor of Thread class.


Code:
public class MultiThreading{
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread myThread = new Thread(myRunnable);
        // Start the thread
        myThread.start();
    }
}

class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.err.println("Current Thread Name: " + Thread.currentThread().getName());
    }
}

Output:
Current Thread Name: Thread-0

Creating object of Thread Class using lambda

A thread can be created by instantiating a Thread object. and passing object of runnable interface using lambda function.
The runnable interface is a functional interface, it has a single abstract method named 'run' which we can execute in a lambda function.

Code:
public class MultiThreading{
    public static void main(String[] args) {
        Thread myThread = new Thread(()->{
            System.err.println("Current Thread Name: "+ Thread.currentThread().getName());
        });
        myThread.start();
    }
}

Output:
Current Thread Name: Thread-0

I hope you find this article helpful, if you have any questions or doubt, please feel free to ask in comment section.
I encourage you to share this with your colleagues and classmates if you found value in this post. 

Thank you

*

Post a Comment (0)
Previous Post Next Post