Creating Multiple Threads In Java
Last Updated : Mar 11, 2024
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"); } }
- You can see that we write the Java code that generates several threads in this section.
- We begin by importing the required package, java.lang.The Thread class must be used by Thread.
- 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.
- 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.
- Moving on, the execution of our programme begins with the Main class.
- We make two instances of the MyThread class in the main() method and assign them to the variables thread1 and thread2, respectively.
- 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.
- 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.
- After that we use join() that causes the main thread to pause until threads 1 and 2 have finished running.
- 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.
- Finally, after both threads have finished running, we print a message with the term "talkerscode" in it indicating that all threads have finished running.
- 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.