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 6912743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:07:49+00:00 2026-05-27T09:07:49+00:00

I am trying to understand the state of a Thread object after the thread

  • 0

I am trying to understand the state of a Thread object after the thread has completed. I read in a book that once a thread has completed its run() method, the thread object will still be a valid object on the head and can still be used as an instance object and you can still call methods on it. I decided to try this with the following example:

class DieThread implements Runnable{

    int y = 0;

    public void run(){
        for(int x=0; x<100; x++){
            System.out.println("             " + Thread.currentThread().getName());
            System.out.println("y is " + y);
            y++;
            System.out.println("y is " + y);
        }
    }   
}

class ZiggyTest2{
    public static void main(String[] args){     

        DieThread d = new DieThread();

        Thread a = new Thread(d);
        Thread b = new Thread(d);
        Thread c = new Thread(d);

        a.start();
        b.start();
        c.start();

        try{
            a.join();
            b.join();
            c.join();
        }catch(Exception e){
            System.out.println(e);
        }

        System.out.println("Running C's Run");
        c.run();        
    }
}

main() waits for the other 3 threads to complete by using join(). It then call c’s run() method but for some reason it is not printing out anything from the run() method. Here is the last few lines of the output after i run the above program.

y is 287
y is 287
y is 288
y is 289
             Thread-1
             Thread-0
y is 289
y is 289
y is 290
y is 291
             Thread-1
             Thread-0
y is 291
y is 291
y is 292
y is 293
             Thread-1
             Thread-0
y is 293
y is 293
y is 294
y is 295
             Thread-0
y is 295
y is 296
             Thread-0
y is 296
y is 297
             Thread-0
y is 297
y is 298
             Thread-0
y is 298
y is 299
             Thread-0
y is 299
y is 300
Running C's Run

Even though the thread has completed, i was expecting the value of y to be 400 because of the c.run() call.

Any ideas?

Thanks

  • 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-05-27T09:07:49+00:00Added an answer on May 27, 2026 at 9:07 am

    The reason for what you are observing is buried in the implementation of the Thread class.

    When you call c.run() you are calling the run method on the thread object. This is specified as calling the run method of the target object (i.e. the runnable supplied in the Thread constructor) if it is not null. If target is null then the Thread.run() method does nothing.

    OK, so you supplied a non-null target.

    In fact, when a Thread finishes, the Thread.exit() method is called to clean up. And one of the things that the cleanup does is to assign null to target. (The code has a comment about needing to aggressively clear references, and a bug id. Unfortunately, the corresponding bug report is missing, so it is not possible to know the real reason(s) that they do this.)

    Anyway, the bottom line is that you can’t rerun the target object’s run() method by calling Thread.run(). But you can call the target object’s run() method directly … if you have the target reference.

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

Sidebar

Related Questions

Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
After trying to understand why client code is not rendered in a page (injected
Just trying to understand that - I have never used it before. How is
I've been trying to understand how Ruby blocks work, and to do that I've
I understand that calling Thread.Abort() is BAD AND WRONG because of various reasons Can
I'm trying to understand when to use anonymous JavaScript functions. State differences between the
I'm trying to understand the MVC pattern and I get the general idea that
I am trying to understand HTML5 history object. Here is a simple example which
After reading the answer here, I understand that, if I register a user control
I am trying to understand the sigsetjmp() and siglongjmp() functions. The man pages state

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.