Q-35: Write a JAVA program to implement the concept of multi-threading by implementing the Runnable interface.
  

Runnable interface


// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science ) class MultiThreading implements Runnable { 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!"); } } } class MultiThreadingDemo { public static void main(String[] args) { int n = 8; // Number of threads for(int i=0; i < n; i++) { Thread object = new Thread(new MultiThreading()); object.start(); } } }

OUTPUT

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

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