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

  • Home
  • SEARCH
  • 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 6629257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:14:03+00:00 2026-05-25T22:14:03+00:00

I have a favorite C# program similar to the one below that shows that

  • 0

I have a favorite C# program similar to the one below that shows that if two threads share the same memory address for counting (one thread incrementing n times, one thread decrementing n times) you can get a final result other than zero. As long as n is reasonably large, it’s pretty easy to get C# to display some non-zero value between [-n, n]. However, I can’t get Java to produce a non-zero result even when increasing the number of threads to 1000 (500 up, 500 down). Is there some memory model or specification difference wrt C# I’m not aware of that guarantees this program will always yield 0 despite the scheduling or number of cores that I am not aware of? Would we agree that this program could produce a non-zero value even if we can not prove that experimentally?

(Not:, I found this exact question over here, but when I run that topic’s code I also get zero.)

public class Counter
{
  private int _counter = 0;

  Counter() throws Exception
  {
    final int limit = Integer.MAX_VALUE;

    Thread add = new Thread()
    {
      public void run()
      {
        for(int i = 0; i<limit; i++)
        {
          _counter++;
        }
      }
    };

    Thread sub = new Thread()
    {
      public void run()
      {
        for(int i = 0; i<limit; i++)
        {
         _counter--;
        }
      }
    };

    add.run();
    sub.run();
    add.join();
    sub.join();

    System.out.println(_counter);
  }

  public static void main(String[] args) throws Exception
  {
    new Counter();
  }
}
  • 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-25T22:14:04+00:00Added an answer on May 25, 2026 at 10:14 pm

    The code you’ve given only runs on a single thread, so will always give a result of 0. If you actually start two threads, you can indeed get a non-zero result:

    // Don't call run(), which is a synchronous call, which doesn't start any threads
    // Call start(), which starts a new thread and calls run() *in that thread*.
    add.start();
    sub.start();
    

    On my box in a test run that gave -2146200243.

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

Sidebar

Related Questions

I have a favorite table containing some fields like login_id,driver_id(One login_id may have many
Does anyone have a favorite style site that posts the latest and greatest sites
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have one custom action (add) and two custom objects (favorites list) and (tv
I have to write an address book program in C# 2008. It is supposed
I have over 300 questions/prompts that I plan to include in the program. The
I have an array that stores the values: var array = [['favorite color'],['black','red']] to
I have a tab bar app with an item that's a favorite list. When
I have two core data entities, Articles and Favorite . Articles has To-Many relationship
I like to watch my favorite TV shows on the go. I have all

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.