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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:54:37+00:00 2026-06-08T15:54:37+00:00

I just wanted to write an example for an race condition: MyParallelClass.java : public

  • 0

I just wanted to write an example for an race condition:

MyParallelClass.java:

public class MyParallelClass implements java.lang.Runnable {
    public int counter = 0;

    @Override
    public void run() {
        if (test.globalVar > 0) {
            for (int i = 0; i < 1000000; i++) {
                counter++;
            }
            test.globalVar--;
        }
    }
}   

test.java:

public class test {
    public static int globalVar;

    public static void main(String[] args) {
        globalVar = 1;

        MyParallelClass a = new MyParallelClass();
        MyParallelClass b = new MyParallelClass();

        new Thread(a).start(); // Thread A
        new Thread(b).start(); // Thread B

        System.out.println(globalVar);
    }
}   

What I thought would happen:

I thought this could output either 0 if thread A was executed completely before Thread B starts.

The variable test.globalVar could also get manipulated like this:

Thread A                     -  Thread B  
checks if (globalVar > 0)
      looping ...               checks if (globalVar > 0)
      looping ...               execute all four bytecode commands of "test.globalVar--;"
      execute test.globalVar--;

so the value of test.globalVar would be -1.

So either one of the if-statements get executed or both.

What actually happened:

I got 0 and 1 as output of the main method. Why do I get 0 and 1 and not 0 and -1?

  • 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-08T15:54:39+00:00Added an answer on June 8, 2026 at 3:54 pm

    The

    System.out.println(globalVar);
    

    does not wait for the threads to complete. The threads may or may not be complete at that point. So the value can be 0, 1, or -1 depending on whether both the threads completed, one completed or both did not complete.

    To have a better test,
    – use Thread.sleep() in the threads to make sure that there is a delay
    – Use different delays in the different threads to better visualize the race condition.
    – You may want to print the value of the variable in the threads also. this way you have three threads racing (A, B and the main thread) and you get better visualizations.

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

Sidebar

Related Questions

Just wanted to write some recursion but can't check if the child is in
I am just a new beginner in MySQL and I wanted to write the
I have to write my own hash function. If I wanted to just make
just wanted to ask where I define initial class properties? From other languages I
I just wanted to know why Excel-2007 record macro does not write anything for
Just wanted to make sure: When I write: require('/path/to/file/file.php'); Is the file required with
Just wanted opinions on a design question. If you have a C++ class than
I just wanted to share this Query class and get your thoughts on it.
Just wanted to know how i would replace sitename.com with sitename2.com whenever a user
Just wanted to ask you about the drawbacks (I mean memory or reponse time)

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.