CAN YOU PLEASE EXPLAIN THIS THREAD WORKING?

Madam Walker

Total Post:22

Points:154
Posted by  Madam Walker
Java 
 1139  View(s)
Ratings:
Rate this:

Heres the code about threads.....I don't 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 ?

  1. Ida B Wells

    Post:30

    Points:210
    Re: Can you please explain this Thread working?

    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!

      Modified On Apr-03-2018 04:31:35 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!