Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:00:06+00:00 2026-06-10T19:00:06+00:00

Given this sample code: Runnable r = new Runnable() { public void run() {

  • 0

Given this sample code:

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

why is the output Dog and not Cat??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:00:08+00:00Added an answer on June 10, 2026 at 7:00 pm

    The implementation of run in Thread simply calls the Runnable provided in the constructor, if there is one. You’re overriding that code, so if the new thread simply has its run method called regardless, the Runnable is ignored. Of course, you should be able to look at the source code to check that… (I’ve just done so, and while I’m not going to post the source here, it does exactly what I’ve described.)

    What you’ve really exposed is an encapsulation problem – Thread shouldn’t have these different, potentially conflicting, ways of saying what the thread should do. Basically, you should almost never extend Thread directly. Just because it’s been designed badly doesn’t mean you have to abuse that poor design 😉

    EDIT: This is actually documented, in a somewhat roundabout way. start() is documented as:

    Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

    And run() is documented as:

    If this thread was constructed using a separate Runnable run object, then that Runnable object’s run method is called; otherwise, this method does nothing and returns.

    So the run() method is called as per start(), but you’ve overridden run(), which is the only method which will call the Runnable provided in the constructor.

    Note that if you’d overridden the method like this:

    Thread t = new Thread(r) {
      public void run() {
        super.run();
        System.out.print("Dog");
      }
    };
    

    Then the output would be “CatDog”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Take this sample code: Class Foo ReadOnly name As String Public Sub New(name As
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Given this code sample: complex.h : #ifndef COMPLEX_H #define COMPLEX_H #include <iostream> class Complex
Given this following sample code which clones a table row, sets some properties and
Given this sample Python code: for item in items: if somecondition(item): requireditem = item
Given this 3D bar graph sample code , how would you convert the numerical
I am working with some sample code generously given to me by this answer
I am using sample code given in SDK . In this demo I observe
I was given this sample code from college today and it worked fine inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.