Advertisement
//class A implementing Runnable interface
class A implements Runnable
{
public void run()
{
for(int i=0;i<3;i++)
System.out.println(Thread.currentThread().getName()); //accessing the reference to the currently
} //executing thread by calling static method
//currentThread() of Thread class.
public static void main(String... ar)
{
A ob= new A();
Thread t1= new Thread(ob,"Thread1");
Thread t2= new Thread(ob,"Thread2");
Thread t3= new Thread(ob,"Thread3"); //Calling Thread's constructor & passing the object
//of A class that implemented Runnable interface
//& the name of new thread.
t1.start(); //Starting the first thread
t2.start(); //Starting the second thread
t3.start(); //Starting the third thread
}
}
Thread1
Thread3
Thread3
Thread3
Thread1
Thread1
Thread2
Thread2
Thread2
Advertisement
//class A implementing Runnable interface
class A implements Runnable
{
public void run()
{
for( int i=0;i<3;i++)
try
{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());//accessing the reference to the
} //currently executing thread by calling
} //static method currentThread() of Thread class.
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String... ar)
{
A ob1= new A();
A ob2= new A();
A ob3= new A();
Thread t1= new Thread(ob1,"ThreadA");
Thread t2= new Thread(ob2,"ThreadB");
Thread t3= new Thread(ob3,"ThreadC"); //Calling Thread's constructor & passing the object
//of A class that implemented Runnable interface
//& the name of new thread.
t1.start(); //Starting the first thread
t2.start(); //Starting the second thread
t3.start(); //Starting the third thread
}
}
ThreadA
ThreadB
ThreadC
ThreadB
ThreadC
ThreadA
ThreadC
ThreadB
ThreadA
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement