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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:42:17+00:00 2026-05-23T07:42:17+00:00

I am just starting to learn about Java concurrency and I am getting some

  • 0

I am just starting to learn about Java concurrency and I am getting some strange output from the following code:

ThreadSafe.java:

public class ThreadSafe {
    private int value;

    public ThreadSafe() {
        value = 0;
    }

    public synchronized int getNext() {
        if (value >= 10000) {
            value = 0;
        }
        return value++;
    }
}

RunnableImpl.java:

public class RunnableImpl implements Runnable {
    private String name;
    private ThreadSafe ts;

    public RunnableImpl(String name, ThreadSafe ts) {
        this.name = name;
        this.ts = ts;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println(name + ": " + ts.getNext());
        }
    }
}

Main.java:

public class Main {
    public static void main(String[] args) {
        ThreadSafe ts = new ThreadSafe();

        Runnable a = new RunnableImpl("A", ts);
        Runnable b = new RunnableImpl("B", ts);

        new Thread(a).start();
        new Thread(b).start();
    }
}

Whenever the threads switch off, I get output like the following:

B: 7320
B: 7321
A: 4278 // What?
A: 7323
A: 7324

Going back up the output where A and B switch off:

A: 4275
A: 4276
A: 4277
B: 2279 // ROBBLE ROBBLE!
B: 4279
B: 4280

I can see that A left off at 4277 earlier on and picked up at 4278 before skipping 7322 and continuing on with 7323. This happens with both value++ and ++value. My understanding was that synchronized would let each thread’s method invocation of getNext() complete before letting any other threads execute that method.

When I use private volatile int value; in threadsafe.java I get output similar to the following:

A: 8511
A: 8512
A: 8513
B: 7022 // Meh.
B: 8514
B: 8515

This time no numbers were skipped (from ++value, still skips with value++) but the output still seems to be using a cached value for the first go.

What am I overlooking here?

  • 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-23T07:42:18+00:00Added an answer on May 23, 2026 at 7:42 am

    Your ThreadSafe class is fine. This is happening most probably because of the thread scheduling. Namely, in the first example, thread “A” got de-scheduled after fetching 4278 from the instance, but before printing it to the terminal.

    Then “B” ran for a while, fetching all values up to 7322, and printing all values up to 7321 before being de-scheduled, at which point “A” was scheduled again, and its next step was to print the value it had previously fetched (4278), and the continuing with the next value from the ThreadSafe class (7323).

    So your problem is that while fetching the numbers is properly thread-safe, printing them is not.

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

Sidebar

Related Questions

I am just starting to learn about the code contracts library that comes standard
im just starting to learn about sockets and i have been given this code,
I'm starting to learn about MVC-based applications being that I'm getting into some heavier
I am just starting to learn JNI. I have been following a simple example,
I am just starting to learn about spring and was looking at the difference
I'm just starting to learn about jQuery and am becoming familiar with how jQuery
I'm just starting to learn about jQuery. I'm more of a html+css web designer
I am just starting to learn a bit about the entity framework and don't
I'm just starting to learn OpenCV programming. May I just ask about how can
I'm just starting to learn Python and have heard about the GIL and how

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.