Can you please explain this Thread working?

Total Post:22

Points:154

Java 
 1404  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. 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

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.