Create thread using Anonymous class:
Anonymous Class
Explanation:- A Class which does not have any name is known as anonymous class.
Q. Write a program to create thread using anonymous class/local class.
class testAno
{
public static void main(String args[])
{
Thread t =new Thread(new runnable(){
public void run()
{
System.out.println("I am in Thread");
}
});
t.start();
}
}
O/P:- I am in Thread
Create Thread Using Interface OR Runnable Interface:
class Method
{
public static void main(String args[])
{
TestThread runnable_obj=new TestThread();
Thread t=new Thread(runnable_obj);
t.start();
}
}
class TestThread implements Runnable
{
public void run()
{
System.out.println("In Thread Main");
}
}
Leave a Reply