Create Thread using Anonymous class and Interface

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");
  }
}

Posted

in

by


Related Posts

Comments

4 responses to “Create Thread using Anonymous class and Interface”

  1. Muzaffar Shah Khan Avatar
    Muzaffar Shah Khan

    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

  2. […] Java Threading – Executor Framework and Callable Interface Posted by Jitendra Zaa on March 24th, 2011 Executer is a concept evolved from the world of Runnable interface. We have seen that how to create a thread using Runnable interface, extending thread and anonymous classes. […]

  3. Kara Thomas Avatar
    Kara Thomas

    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

  4. Joseph Porter Avatar

    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/

Leave a Reply to Joseph PorterCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading