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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:33:28+00:00 2026-06-01T13:33:28+00:00

In an effort to learn about synchronization via Java, I’m just messing around with

  • 0

In an effort to learn about synchronization via Java, I’m just messing around with some simple things like creating a counter shared between threads.

The problem I’ve run into is that I can’t figure out how to print the counter sequentially 100% of the time.

int counterValue = this.counter.incrementAndGet();
System.out.println(this.threadName + ": " + counterValue);

The above increments the AtomicInteger counter, gets the new value, and prints it to the console identified by the thread name that is responsible for that update. The problem occurs when it appears that the incrementAndGet() method is causing the JVM to context switch to another thread for its updates before printing the current thread’s updated value. This means that the value gets incremented but not printed until the thread returns to the executing state. This is obvious when looking at this example output:

Thread 3: 4034
Thread 3: 4035
Thread 3: 4036
Thread 1: 3944
Thread 1: 4037
Thread 1: 4039
Thread 1: 4040
Thread 2: 3863
Thread 1: 4041
Thread 1: 4043

You can see that when execution returns to Thread 1, it prints its value and continues updating. The same is evident with Thread 2.

I have a feeling that I’m missing something very obvious.

  • 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-01T13:33:30+00:00Added an answer on June 1, 2026 at 1:33 pm

    The problem occurs when it appears that the incrementAndGet() method is causing the JVM to context switch to another thread for its updates before printing the current thread’s updated value

    This is a race condition that often can happen in these situations. Although the AtomicInteger counters are being incremented properly, there is nothing to stop Thread 2 from being swapped out after the increment happens and before the println is called.

    int counterValue = this.counter.incrementAndGet();
    // there is nothing stopping a context switch here
    System.out.println(this.threadName + ": " + counterValue);
    

    If you want to print the “counter sequentially 100% of the time” you are going to have to synchronize on a lock around both the increment and the println call. Of course if you do that then the AtomicInteger is wasted.

    synchronized (counter) {
        System.out.println(this.threadName + ": " + counter.incrementAndGet());
    }
    

    If you edit your question to explain why you need the output to be sequential maybe there is a better solution that doesn’t have this race condition.

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

Sidebar

Related Questions

In the effort to learn more about applets and Java, I am experimenting making
In an effort to learn more about networking I'd like to do an exercise:
I'm putting in some effort to learn Python, and I am paying close attention
In my effort to learn more about cross-platform developping in C# I tried checking
In a seemingly never ending effort to learn more about iphone development, I have
In an effort to learn more about php and mysql, I have been working
In my ongoing effort to learn more about python, I am trying to add
I've never used TDD and unit testing properly, but would like to learn some
I am making the effort to learn Vim. When I paste code into my
I am an experienced Java developer who is trying to learn web development with

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.