• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is there any benefit for doing this

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ClassThread
{

public void print()
{
System.out.println("you are inside print() method");

}

public static void main(String args[])
{
ClassThread ct=new ClassThread();

Runnable ra=new Runnable(){
public void run()
{
ct.print();
}
};
Thread t=new Thread(ra, "ThreadA");

}

}


What is the special advntage of using local class Runnable instead of class
ClassThread impementing Runnable and overidding run() method?
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
easy: if you extend Thread and overwrite the run() method, you don't have the option to extend any other class. while this is not important for hello-world threads, in real-life applications you'll most probably want your thread (small t) to exend some sort of business-class.

hope it helped,
jan
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, is this how real world applications are written?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Jan said is true, but I don't think s/he looked carefully at the code. The question is not about extending Thread or using Runnable; the question is about using an anonymous Runnable class rather than making "ClassThread" implement Runnable itself.

In this little program, it makes no difference. In a real world program, you might do this if there are a number of different kinds of Runnables that need to work with ClassThread objects. If, on the other hand, you really are implementing the notion of "ClassThread running in a Thread", then the anonymous Runnable is just sillt, and ClassThread should itself implement Runnable. Anonymous classes aren't pretty, and using them for no reason just clutters your code.
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest...So, if public void run() method has 2 or more implementations then anonymous classes should be used. Thats what you mean?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean that if you need two or more "run" methods, then they have to be in separate classes, and if they're very short, then an anonymous class is a convenient way to do it. There's no "should", though; if you want to use a named class, that's just fine too.
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest, I am not sure if i am right in understanding you. When you say in two different class, you mean that two differnt classes implementing Runnable interface and overridding run() method in them?.... if it is not what ur saying then i am really very sorry and i believe i understood wrong.
reply
    Bookmark Topic Watch Topic
  • New Topic