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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:05:12+00:00 2026-05-31T20:05:12+00:00

I have an example that seems strange to me. public class Join { public

  • 0

I have an example that seems strange to me.

public class Join {
    public static void main(String[] args) {
        Thread t1 = new Thread(
                new Runnable() {
                    public void run() {
                        System.out.println(Thread.currentThread().getName());
                    }
                }
        );
        Thread t2 = new Thread(t1);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        try {t1.join();} catch (InterruptedException ie) {}
        t2.start();
    }
}

We’ll see printed only t1.
If we’ll comment “t1.join”, we’ll se the expected output (t1 t2).
Why ?

  • 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-31T20:05:13+00:00Added an answer on May 31, 2026 at 8:05 pm

    The second thread is created incorrectly:

    Thread t2 = new Thread(t1);
    

    I can’t support it by documentation, but in the source code of Thread.run() I see:

    if (target != null) {
        target.run();
    }
    

    Where target is Runnable instance. When the Thread is done, it clears the target variable:

    private void exit() {
      //...
      target = null;
    

    This means that when the first thread is done (join() method) it clears the target and the second thread does nothing. When join() is removed, both access the same target of t1 (race condition).

    TL;DR

    Never create a thread using another thread instance (even though it implements Runnable). Instead create a separate Runnable and pass it:

    final Runnable run = new Runnable() {
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    };
    Thread t1 = new Thread(run, "t1");
    Thread t2 = new Thread(run, "t2");  //new Thread(t1, "t2"); is incorrect!
    t1.start();
    t2.start();
    

    You don’t need any join()s here, by default these are non-daemon threads.

    See also

    • Java Threading Basics (discussion on #2)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an example function below that reads in a date as a string
I have come across a strange behavior of Java that seems like a bug.
We have a model association that looks something like this: class Example < ActiveRecord::Base
I have a class that needs access to urllib2, the trivial example for me
Anyone have example Rx code that shows how to execute an action due to
HI I have an example document that looks like <ItemEntry> <PurchaseDate>2010-03-18T20:36:32.81108+13:00</PurchaseDate> <StoreGUID>0a0324ad-5f99-486a-a2d0-870bc6991e9f</StoreGUID> <ExpiryDate />
I have this very simple example that I am using to learn structs in
I have this code (an example that PHP generates for me)... <select id=outsideBlue3> <option
Does anyone have an example of script that can work reliably well across IE/Firefox
I have an app based on the CoreDataBooks example that uses an addingManagedObjectContext to

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.