How To Perform Single Task By Multiple Threads In Java
In Java, if we have to perform a single task by many threads, we need to use only one run () method.
Performing single task by multiple threads
Let’s see an example, given below.
Code
- public class MultipleThreads extends Thread {
- public void run() {
- System.out.println("Task one is start now..");
- }
- public static void main(String args[]) {
- MultipleThreads mt1 = new MultipleThreads();
- MultipleThreads mt2 = new MultipleThreads();
- MultipleThreads mt3 = new MultipleThreads();
- mt1.start();
- mt2.start();
- mt3.start();
- }
- }
Output
In the example, mentioned above example, we created multipleThreads class extends thread class and performed a single task through the multiple threads.
Let’s see another example with Runnable interface, mentioned blow.
Code
- public class MultipleThreads implements Runnable {
- public void run() {
- System.out.println("Task one is start now….");
- }
- public static void main(String args[]) {
- Thread t1 = new Thread(new MultipleThreads()); //passing annonymous object
- Thread t2 = new Thread(new MultipleThreads());
- Thread t3 = new Thread(new MultipleThreads());
- t1.start();
- t2.start();
- t3.start();
- }
- }
Output
In the example, mentioned above, we create multipleThreads class, implement the Runnale interface instead of extending threads and perform a single task through the multiple threads.
Each thread runs within a separate callstack in multithreading.
How to perform multiple tasks by multiple threads in Java
In Java, if we have to perform multiple tasks by many threads, we need to use multiple run () method.
Performing multiple task by multiple threads
Let’s see an example, given below.
Code
- class Multi1 extends Thread {
- public void run() {
- System.out.println("Task one is start now...");
- }
- }
- class Multi2 extends Thread {
- public void run() {
- System.out.println("Task two is start now...");
- }
- }
- public class MultipleThreads {
- public static void main(String args[]) {
- Multi1 t1 = new Multi1();
- Multi2 t2 = new Multi2();
- t1.start();
- t2.start();
- }
- }
Output
In the example, mentioned above, we created three classes and two classes extend the thread class to perform the multiple tasks.
Let’s see another example- annonymous class with Runnable interface, given below.
Code
- public class MultipleThreads {
- public static void main(String args[]) {
- Runnable r1 = new Runnable() {
- public void run() {
- System.out.println("Task one is start now...");
- }
- };
- Runnable r2 = new Runnable() {
- public void run() {
- System.out.println("Task two is start now...");
- }
- };
- Thread t1 = new Thread(r1);
- Thread t2 = new Thread(r2);
- t1.start();
- t2.start();
- }
- }
Output
In the example, mentioned above, we created MultipleThreads class, use Runnable interface to perform the multiple tasks.
Let’s see another example- anonymous class that extends thread class, given below.
Code
- public class MultipleThreads {
- public static void main(String args[]) {
- Thread t1 = new Thread() {
- public void run() {
- System.out.println("Task one is start now...");
- }
- };
- Thread t2 = new Thread() {
- public void run() {
- System.out.println("Task two is start now...");
- }
- };
- t1.start();
- t2.start();
- }
- }
Output
In the example, mentioned above, we created MultipleThreads class, extended the thread class to perform the multiple tasks.
Summary
Thus, we learnt, if we have to perform a single task by many threads, we need to use only one run () method and also learnt, if we have to perform multiple tasks by many threads, we need to use multiple run () method.