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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:42:25+00:00 2026-05-28T15:42:25+00:00

I am reading Java Thread Programming a book by Paul Hyde .I am into

  • 0

I am reading Java Thread Programming a book by Paul Hyde.I am into intial chapter. I am running various sample programs in Eclipse IDE.

public class TwoThreadAlive extends Thread {
public void run() {
    for (int i = 0; i < 10; i++) {
        printMsg();
    }
}

public void printMsg() {
    // get a reference to the thread running this
    Thread t = Thread.currentThread();
    String name = t.getName();
    System.out.println("name=" + name);
}

public static void main(String[] args) {
    TwoThreadAlive tt = new TwoThreadAlive();
    tt.setName("my worker thread");

    System.out.println("before start(), tt.isAlive()=" + tt.isAlive());
    tt.start();
    System.out.println("just after start(),tt.isAlive()=" + tt.isAlive());

    for (int i = 0; i < 10; i++) {
        tt.printMsg();
    }

    System.out
            .println("at the end of main(), tt.isAlive()=" + tt.isAlive());
}}

This program is giving SAME output every time I run it.But it has mentioned that output will differ as JVM will be busy in context switching.Can you please tell me that why it is giving same output?

And it is giving

at the end of main(), tt.isAlive()=true

This should have been

at the end of main(), tt.isAlive()=false

Please help me , this is making me mad.

  • 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-28T15:42:26+00:00Added an answer on May 28, 2026 at 3:42 pm

    Nope, the output is correct because by the time the main method executes the last println there is no guarantee that also your thread has finished executing. Add a tt.join(); right before the last print:

    tt.join();
    
    System.out
            .println("at the end of main(), tt.isAlive()=" + tt.isAlive());
    

    Now the main thread will be blocked until tt finishes, and so the println will execute only after tt is dead.

    As for your output being always the same, 10 is a too fine grain value and most likely each thread finishes printing everything by the time it gets context switched. Try with a much larger value.

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

Sidebar

Related Questions

I remember reading in some Java book about any operator other than 'instanceof' for
I've been reading up on how to use Thread in java, and I'm hoping
I am reading Java Concurrency in Practice and kind of confused with the thread
I was reading a thread here about the performance of java ArrayList and LinkedList.
So I am reading this book titled Java Concurrency in Practice and I am
I am reading this book called Java Concurrency in Practice and the author gives
I want to make a program in java in which one thread is reading
I was reading some java thread interruption and I don't understand some stuff. Hope
I am new to Hibernate, reading this book Java persistence with Hibernate and I
Possible Duplicate: Thread safety in Java class I'm reading Java concurrency in Practice ,

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.