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"); } }
I used anonymous class usually in J2ME gaming and swings
In swings when we populate data from db the gui gets hanged
To avoid this the entire call to populate was written in anonymous class for thread
In other words anonymous class is useful to write Thread independently on the fly in the class
Thanks correct me if i am wrong
Hey I’m a homeless person advocate from mississippi I help my local homeless people I have started a paypal account for people who want to send donations to help homeless people and stimulate the economy if you have a little extra money and you want to help the homeless send a donation to helpingthehomeless08@gmail.com even if you don’t have it I understand this economy isn’t the best thank you anyway and god bless you all
That’s a good one, didn’t think of using a thread with an anonymous class, thanks!! learn something new everyday.
Also a nice one:
http://www.programmerinterview.com/index.php/java-questions/anonymous-class-interface/