Simple Multithreading Demo Program invokes the method(s) of Runnable interface Thread class
A simple demo program in Java that demonstrates the usage of the Thread class. The program creates two threads that perform different tasks concurrently.
Here’s an example that demonstrates the usage of Thread class and creation of threads:
// Runnable implementation class MyRunnable implements Runnable { @Override public void run() { String name=Thread.currentThread().getName(); System.out.println(name+" running"); System.out.println(name+" Priority="+Thread.currentThread().getPriority()); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + " interrupted"); return; } System.out.println(Thread.currentThread().getName() + " finished"); } } // Thread subclass class MyThread extends Thread { String name; public MyThread(String name) { super(name); this.name=name; } @Override public void run() { System.out.println(Thread.currentThread().getName() + " running"); // Print the priority of the thread System.out.println(name+" Priority= " + getPriority()); // Sleep for a while try { Thread.sleep(2000); } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + " interrupted"); return; } System.out.println(Thread.currentThread().getName() + " finished"); } } public class ThreadDemo { public static void main(String[] args) { // Create a thread using a Runnable object Thread thread1 = new Thread(new MyRunnable(), "Mahesh"); // Create a thread by extending the Thread class Thread thread2 = new MyThread("Pani"); // Set the priority of thread1 to MAX_PRIORITY thread1.setPriority(Thread.MAX_PRIORITY); // Start both threads thread1.start(); thread2.start(); // Sleep for a while try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } try { thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("All threads completed"); } }
Output:
D:\>javac ThreadDemo.java
D:\>java ThreadDemo
Mahesh running
Pani running
Pani Priority= 5
Mahesh Priority=10
Mahesh finished
Pani finished
All threads completed
D:\Threading>java ThreadDemo
Pani running
Mahesh running
Mahesh Priority=10
Pani Priority= 5
Mahesh finished
Pani finished
All threads completed
D:\BhavaThreading>java ThreadDemo
Mahesh running
Pani running
Pani Priority= 5
Mahesh Priority=10
Mahesh finished
Pani finished
All threads completed
*/
In this example, we’ve added the following functionalities:
- Setting the name of the threads by passing the desired names as arguments to the Thread constructor.
- Setting the priority of thread1 to MAX_PRIORITY using the setPriority() method.
- Getting the name of the current thread using Thread.currentThread().getName().
- Printing the priority of the thread using getPriority().
- Using the sleep() method to pause the execution of a thread for a specified duration.
When you run this program, you’ll see the output demonstrating these functionalities, including setting and getting the thread names, setting the priority, using the sleep() method and printing related information.
Please note that the priority values depend on the platform and may not have a significant effect on thread scheduling in all cases. Also, keep in mind that the Thread.sleep() method may throw an InterruptedException, which you should handle appropriately in real-world scenarios.