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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:35:41+00:00 2026-05-23T01:35:41+00:00

In the below code the answer is always Started 0 1 2 3 Complete.

  • 0

In the below code the answer is always Started 0 1 2 3 Complete. Im just wondering how it is possible.

If someone could help with the consistency of the output, it would be nice

public class TestOne extends Thread {

/**
 * @param args
 */
public static void main(String[] args)throws Exception {
    // TODO Auto-generated method stub

    Thread t = new Thread(new TestOne());
    t.start();
    System.out.println("started");
    t.join();
    System.out.println("Complete");




}

public void run(){
    for(int i=0;i<4;i++){
        System.out.println(i);
    }
}
  • 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-23T01:35:41+00:00Added an answer on May 23, 2026 at 1:35 am

    Most likely you’re getting the same results because, most of the time, the main thread starts a new thread then, before that new thread has a chance to print anything, the main thread prints its started message. The join in the main thread then forces it to wait until the other thread has finished, then it prints Complete.

    You have a race condition here. The instant you start the second thread, it’s indeterministic as to which order the lines will be output (other than the complete line which is made deterministic by virtue of the wait call, as previously mentioned).

    But a race condition doesn’t guarantee that you’ll get different results on multiple runs, only that it is possible. You still shouldn’t rely on that behaviour, of course.

    For example, the following code:

    public class TestOne extends Thread {
        public static void main (String[] args) throws Exception {
            Thread t = new Thread (new TestOne());
            t.start();
    
            Thread.sleep (1000);  // <- Added this.
    
            System.out.println ("Started");
            t.join();
            System.out.println ("Complete");
        }
    
        public void run() {
            for (int i = 0; i < 4; i++) {
                System.out.println (i);
            }
        }
    }
    

    will output:

    0
    1
    2
    3
    Started
    Complete
    

    Although, even then, the order is not guaranteed as it may take more than a second for the thread to “warm up” – sleep is rarely a good solution to race conditions. I’ve just used it here for illustrative purposes.

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

Sidebar

Related Questions

Update: Solved, with code I got it working, see my answer below for the
EDIT: See my working code in the answers below. In brief: I have a
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
The below code is very simple. I have a jQuery autocomplete bound to an
My below code works fine and is used to populate a <select> item with
The below code works when there are a few element in the To list
The below code in Java throws Null pointer exception. public class New{ int i;
The below code seems like it should work; however, the blob in the database
The below code is working great for handling errors for text fields in my
When running the below code a type is never returned, despite there being a

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.