Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Core Java program to create multiple threads. We can create threads in two ways as below:
- By implementing the Runnable interface.
- By extending the Thread class
The user can select any of the approaches based on their requirement.
- If the requirement is to obtain the basic Thread functionality, extending Thread would be a good choice.
- If we need to extend any other class apart from Thread for other functionalities, we can go with implementing the Runnable interface.
Let’s look at the program for implementing the Runnable interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.clearurdoubt.threads; /** * * @author Sai Gowtham Badvity * Implementing the Runnable interface * */ class RunnableThread implements Runnable { Thread thread; //Internal Thread class for creating a thread instance public RunnableThread() { thread = new Thread(this, "Runnable Thread"); // Create a new Thread and name it as "Runnable Thread" System.out.println("Starting new thread."); thread.start(); // Starts the thread execution by calling the run() method } public void run() { try { for(int i=5; i>0; i--) { System.out.println("Runnable Thread: " + i); Thread.sleep(1000); // Stop execution for 1 sec } System.out.println("Runnable Thread exiting."); } catch(InterruptedException ie) // sleep() method may throw InterruptedException { System.out.println("RunnableThread got interrupted."); } } } public class RunnableThreadDemo { // RunnableThreadDemo class public static void main(String[] args) { new RunnableThread(); // Create the instance of Runnable Thread. It will start a new thread execution. try { for(int i=5; i>0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } System.out.println("Main Thread exiting."); } catch(InterruptedException ie) { System.out.println("Main Thread got interrupted."); } } } |
Output:
Let’s look at the program for extending a Thread class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.clearurdoubt.threads; /** * * @author Sai Gowtham Badvity * Extending the Thread class * */ class ThreadExtender extends Thread { public ThreadExtender() { super("Thread Extender"); // Creates a new Thread with name "Thread Extender" System.out.println("Starting new thread."); start(); // Starts the thread execution by calling the run() method } public void run() { try { for(int i=5; i>0; i--) { System.out.println("Thread Extender: " + i); Thread.sleep(1000); // Stop execution for 1 sec } System.out.println("Thread Extender exiting."); } catch(InterruptedException ie) // sleep() method may throw InterruptedException { System.out.println("ThreadExtender got interrupted."); } } } public class ThreadExtenderDemo { // ThreadExtenderDemo class public static void main(String[] args) { new ThreadExtender(); // Create the instance of ThreadExtender . It will start a new thread execution. try { for(int i=5; i>0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } System.out.println("Main Thread exiting."); } catch(InterruptedException ie) { System.out.println("Main Thread got interrupted."); } } } |
Output:
Happy Learning :).
Please leave a reply in case of any queries.