All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Creating Multiple Threads In Java

Last Updated : Mar 11, 2024

Creating Multiple Threads In Java

In this article we will show you the solution of creating multiple threads in java, Java's ability to create numerous threads enables the concurrent execution of tasks, allowing for the effective use of system resources.

The Runnable interface and Thread class in Java both offer ways to accomplish this.

Developers can either implement the Runnable interface or enhance the Thread class to generate many threads by overriding the run() method. The start() method can be used to start threads, and because each thread runs separately, tasks can be completed in parallel.

This multi-threading functionality improves Java applications' performance, responsiveness, & scalability by enabling effective multi-core processor utilisation and concurrent programming paradigm facilitation.

We'll talk about the concept of using multiple threads in Java now.

Step By Step Guide On Creating Multiple Threads In Java :-

import java.lang.Thread;
class MyThread extends Thread {
    public void run() {
        System.out.println("Hello from " + Thread.currentThread().getName() + " - talkerscode");
    }
}
public class Main {
    public static void main(String[] args) {
        // Creating multiple threads
        MyThread thread1 = new MyThread();
        MyThread thread2 = new MyThread();
        // Starting the threads
        thread1.start();
        thread2.start();
        // Main thread continues execution
        System.out.println("Hello from " + Thread.currentThread().getName() + " - talkerscode");
        // Wait for the threads to complete execution
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("All threads have finished execution - talkerscode");
    }
}
  1. You can see that we write the Java code that generates several threads in this section.
  2. We begin by importing the required package, java.lang.The Thread class must be used by Thread.
  3. We next create a brand-new class named MyThread, which is an extension of the Thread class. This enables us to modify the thread's behaviour by modifying its run() method.
  4. We only use System.out.println() to print a message to the console inside the run() method. We call Thread.currentThread().getName() to get the name of the current thread, which is included in the message. The word "talkerscode" is also included to the message.
  5. Moving on, the execution of our programme begins with the Main class.
  6. We make two instances of the MyThread class in the main() method and assign them to the variables thread1 and thread2, respectively.
  7. By calling the start() method on each thread object, we begin the threads' execution. This causes the MyThread class's run() method, which is specified to run simultaneously with the main thread.
  8. The main thread keeps running while the threads are active and print a message resembling the one from the run() method. It contains the phrase "talkerscode" as well as the name of the current thread.
  9. After that we use join() that causes the main thread to pause until threads 1 and 2 have finished running.
  10. We catch the exception and output the stack trace for debugging purposes if any interruptions, such as an InterruptedException, occur while the threads are running.
  11. Finally, after both threads have finished running, we print a message with the term "talkerscode" in it indicating that all threads have finished running.
  12. When you execute this programme, you will see messages from many threads with their names and the word "talkerscode" next to them.

Conclusion :-

As a result, we have successfully acquired the knowledge necessary to create many threads in Java.

We also discovered that using several threads in Java enables task execution in parallel, improving efficiency and resource use.

The Runnable interface or extending the Thread class are two ways that developers can build unique thread behaviours.

The start() method allows for parallel execution by allowing threads to be started.

I hope this article on creating multiple threads in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪