Java Program on Runnable Interface in Multithreading
Get Job-ready: Java Course with 45+ 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 you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

