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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:57:44+00:00 2026-06-13T00:57:44+00:00

static int i = 0; static void Main() { ThreadTest tt = new ThreadTest();

  • 0
static int i = 0;

static void Main()
{
    ThreadTest tt = new ThreadTest();
    new Thread(tt.Incr).Start();
    tt.I();
    tt.I2();
}

void Incr()
{
    for(int x = 0; x < 3; x++)
    {
        Console.WriteLine(i);
        i++;
    }
}

void I()
{
    while(i <= 3)
    {
        if(i==3)
            break;
        Console.WriteLine("Value of i in I:{0}",i);
    }
}

void I2()
{
    Console.WriteLine("\t\tFinally i is:{0}\n\n",i);
}

I have run this piece of code about a few hundred times now and find that I2 always executes last. Why does this happen? May be a few hundred times is not enough to see the true unpredictability of threads?

Output of 11 runs

  • 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-13T00:57:46+00:00Added an answer on June 13, 2026 at 12:57 am

    Well, I2() is the last method in Main() and it is not threaded in any way.

    So what’s the question, why the thread finishes earlier?

    That’s because I2() is run after I() and the while-loop in I() effectively waits for the thread to finish first.

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

Sidebar

Related Questions

public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
public class MultiplicationTable { public static void main (String[]a){ int[] x; x = new
public class SelfCallingTest { private static int counter; public void SelfCallingMethod(int counter) { Console.WriteLine(The
Going through this article class ThreadTest { static void Main() { Thread t =
class Program { static void Main() { Console.WriteLine(enter main method); Console.WriteLine(Foo.X); Console.ReadKey(); } }
I had this code: int foo(void){ return 1; } int main(void){ static const int
Consider following program: static void Main (string[] args) { int i; uint ui; i
I have some code static void Main(string[] args) { int j = 0; for
public class Sample { public static void main(String[] args) { int year = 2012;
public class I { public static void main(String args[]) { int i=0,j=1,k=2; i=i++ -

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.