Java Program on Runnable Interface in Multithreading
Free Java courses with 37 real-time projects - Learn Java
Program 1
package dataflair; public class MyThread implements Runnable { public void run() { try { int i; for(i=1;i<=10;i++) { System.out.println(i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println(e); } } }
Program 2
package dataflair; public class TestMain { public static void main(String[] args) throws InterruptedException { //MyThread mt1=new MyThread(); Runnable mt1=new MyThread(); Runnable mt2=new MyThread1(); Thread t=new Thread(mt1); Thread t1=new Thread(mt2); // // System.out.println("Main Thread Name: "+Thread.currentThread().getName()); // System.out.println(t.getName()); //t.setName("My First Thread"); // System.out.println(t.getName()); t.start(); t1.start(); // t.join(); // t1.join(); // for (int i = 100; i < 110; i++) // { // System.out.println("Main Thread- "); // Thread.sleep(1000); // } } }
Program 3
package dataflair; public class MyThread1 implements Runnable { public void run() { try { int i; for(i=50;i<=60;i++) { System.out.println(i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println(e); } } }
Did we exceed your expectations?
If Yes, share your valuable feedback on Google