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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:56:39+00:00 2026-06-07T07:56:39+00:00

I am trying to reproduce Thread Interference scenario .. but something is not right.

  • 0

I am trying to reproduce Thread Interference scenario .. but something is not right. Please help me understand what

Main

public static void main(String args[]) throws InterruptedException {

    Counter c = new Counter();

    for (int i = 0; i < 1000000; i++) {
        new T1(c).start();
        new T2(c).start();
    }

    System.out.println(c.value()); // <-- Expect this to sometimes not be 0
}

Counter

class Counter {
    private int c   = 0;

    public void increment() { // <-- intentionally not synchronized 
        c++;
    }

    public void decrement() { // <-- intentionally not synchronized 
        c--;
    }

    public int value() {
        return c;
    }
}

Thread 1

public class T1 extends Thread {

    Counter c;

    T1(Counter c) {
        this.c = c;
    }

    public void start() {
        c.decrement(); // <-- Decrement
    }
}

Thread 2

public class T2 extends Thread {

    Counter c;

    T2(Counter c) {
        this.c = c;
    }

    public void start() {
        c.increment();  // <-- Increment
    }
}

As i am starting 1000000 threads each of which is operating on non-synchronized section of code, i would expect some operations to overlap.

Interference happens when two operations, running in different
threads, but acting on the same data, interleave. This means that the
two operations consist of multiple steps, and the sequences of steps
overlap.

It might not seem possible for operations on instances of Counter to
interleave, since both operations on c are single, simple statements.
However, even simple statements can translate to multiple steps by the
virtual machine. We won’t examine the specific steps the virtual
machine takes — it is enough to know that the single expression c++
can be decomposed into three steps:

Retrieve the current value of c. Increment the retrieved value by 1.
Store the incremented value back in c.

What am i missing please?

  • 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-07T07:56:42+00:00Added an answer on June 7, 2026 at 7:56 am

    As it stands your program is not actually doing anything concurrently, everything is being done in the main thread. Since you override the start method and never call the superclass version, then you are not going to get the functionality to get the new thread running.

    But just calling super.start() won’t fix the problem. Consider start is run by the current thread, so even if you manage to start the thread, the increment/decrement will be executed by the thread calling start(), and there is no concurrent access and modification going on.

    I’d move the increment/decrement stuff out of the start method (deleting your overridden version of it) and override the run method, putting it there instead. Of course, consider using Runnable instead of Thread next time, with Runnable it’s harder to make this kind of error.

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

Sidebar

Related Questions

I am trying to reproduce something that System.Xml.Serialization already does, but for a different
I'm trying to reproduce Fractals without a computer but with a computer instead of
I am trying to reproduce the following but my using rows from the database
I'm trying to reproduce the simple window interface objects in C# XNA like labels,
I am trying to reproduce a simple jquery slider example that works on this
I'm trying to reproduce the effect our friend @Dr. Molle told us on this
I'm trying to reproduce the layout of some paper forms in a WPF application.
I'm trying to reproduce the functionality of the Text annotation at http://mandarinspot.com/annotate and I
I'm trying to reproduce the heatmap presented on this blog by following their tutorial,
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect

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.