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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:35:39+00:00 2026-06-05T21:35:39+00:00

can someone tell me the order in which a thread starts to execute?. I

  • 0

can someone tell me the order in which a thread starts to execute?. I have written the following code

class NewThread implements Runnable {
    Thread t;
    NewThread() {
        //creating a second thread.
        t=new Thread(this,"Demo Thread");
        System.out.println("Child Thread:"+t);
        t.start();
    }

    public void run() {
        try {
            for(int i=0;i<5;i++) {
                System.out.println("Child Thread:"+i);
                Thread.sleep(3000);
            }
        } catch(Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        System.out.println("Exiting Child Thread");
    }
}

and this

public class ThreadDemo {
    public static void main(String args[]) {
        new NewThread();
        try {
            for(int i=0;i<5;i++) {
                System.out.println("Main Thread:"+i);
                Thread.sleep(3000);
            }
        } catch(Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        System.out.println("Exiting Main Thread");
    }
}

when I execute this code, I get many different sets of output.

Child Thread:Thread[Demo Thread,5,main]
Main Thread:0
Child Thread:0
Child Thread:1
Main Thread:1
Main Thread:2
Child Thread:2
Main Thread:3
Child Thread:3
Main Thread:4
Child Thread:4
Exiting Main Thread
Exiting Child Thread

another one,

Child Thread:Thread[Demo Thread,5,main]
Main Thread:0
Child Thread:0
Child Thread:1
Main Thread:1
Child Thread:2
Main Thread:2
Child Thread:3
Main Thread:3
Child Thread:4
Main Thread:4
Exiting Child Thread
Exiting Main Thread

Why is this occurring? Will the thread order never be same?? and it would be nice, if someone can give me pointers to basics of threading and examples.
P.S: I am new to threads and this is my first thread program. Thanks in advance.

  • 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-05T21:35:41+00:00Added an answer on June 5, 2026 at 9:35 pm

    when I execute this code, I get many different sets of output.

    This is expected. The order is not defined and is subject to race conditions as the threads start running and are subjected to thread scheduling by the OS.

    The whole reason why we write multi-threaded applications is that the threads are asynchronous and run in separate processors for speed reasons. To guarantee specific output order, you could synchronize between the threads with locks and the like but you would then lose performance and the whole reason for forking threads would be diminished.

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

Sidebar

Related Questions

Can someone tell me whether MSMQ (using transactions) supports competing consumers? Basically, I have
Can someone tell me of a way in which I can add a user
I have the following query which is adding contraint. but in order to add,
... It's not as silly as it sounds... I have the following code, which
Can someone tell me which are the main differences, or aspects that I should
Can someone tell me why this processes all the files and then does it
Can someone tell me why when I cast a string of say 00332 I
Can someone tell me how I can make a table be 100% height in
Can someone tell me the difference between: $(document).ready(function() { }); and: var someVar =
Can someone tell me why this http://hupcapstudios.com/projects/calendar.html won't load in IE? The error message:

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.