Q-34: Write a JAVA program to implement the concept of multi-threading by extending Thread class.
  

thread class


// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science ) class MultiThreading extends Thread { public void run() { try { // Displaying the thread that is running System.out.println("Thread " + Thread.currentThread().getId()+ " is running"); } catch (Exception e) { System.out.println("Running Thread Interrupted due to some error!"); } } } public class MultiThreadingDemo { public static void main(String[] args) { int n = 8; // Number of threads for(int i=0; i < n; i++) { MultiThreading object = new MultiThreading(); object.start(); } } }

OUTPUT

cmd->  javac  MultiThreadingDemo.java
cmd->  java  MultiThreadingDemo

Thread 15 is running
Thread 22 is running
Thread 17 is running
Thread 18 is running
Thread 16 is running
Thread 19 is running
Thread 20 is running
Thread 21 is running