Home > DeveloperSection > Forums > Can you please explain this Thread working?
Madam Walker
Madam Walker

Total Post:22

Points:154
Posted on    October-14-2013 3:31 AM

 Java Java 
Ratings:


 1 Reply(s)
 674  View(s)
Rate this:

Heres the code about threads.....I dont know the difference between those 2 new() statements.

  Runnable r=new Runnable() {
    public void run() {
    System.out.print("Cat");
    }
    };
    Thread t=new Thread(r) {
    public void run() {
    System.out.println("Dog");
    }
    };
    t.start();

Output is Dog But why and how ?



Ida B Wells
Ida B Wells

Total Post:30

Points:210
Posted on    October-14-2013 4:03 AM

Because you override Thread#run(), so this method is eventually executed when you start the thread. The default Thread#run() delegates to the passed-in Runnable. Rule of thumb: Either provide a Runnable or override Thread#run(), but don't do both!


Don't want to miss updates? Please click the below button!

Follow MindStick