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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:55:15+00:00 2026-05-31T16:55:15+00:00

How do atomic / volatile / synchronized work internally? What is the difference between

  • 0

How do atomic / volatile / synchronized work internally?

What is the difference between the following code blocks?

Code 1

private int counter;

public int getNextUniqueIndex() {
    return counter++; 
}

Code 2

private AtomicInteger counter;

public int getNextUniqueIndex() {
    return counter.getAndIncrement();
}

Code 3

private volatile int counter;

public int getNextUniqueIndex() {
    return counter++; 
}

Does volatile work in the following way? Is

volatile int i = 0;
void incIBy5() {
    i += 5;
}

equivalent to

Integer i = 5;
void incIBy5() {
    int temp;
    synchronized(i) { temp = i }
    synchronized(i) { i = temp + 5 }
}

I think that two threads cannot enter a synchronized block at the same time… am I right? If this is true then how does atomic.incrementAndGet() work without synchronized? And is it thread-safe?

And what is the difference between internal reading and writing to volatile variables / atomic variables? I read in some article that the thread has a local copy of the variables – what is that?

  • 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-31T16:55:16+00:00Added an answer on May 31, 2026 at 4:55 pm

    You are specifically asking about how they internally work, so here you are:

    No synchronization

    private int counter;
    
    public int getNextUniqueIndex() {
      return counter++; 
    }
    

    It basically reads value from memory, increments it and puts back to memory. This works in single thread but nowadays, in the era of multi-core, multi-CPU, multi-level caches it won’t work correctly. First of all it introduces race condition (several threads can read the value at the same time), but also visibility problems. The value might only be stored in “local” CPU memory (some cache) and not be visible for other CPUs/cores (and thus – threads). This is why many refer to local copy of a variable in a thread. It is very unsafe. Consider this popular but broken thread-stopping code:

    private boolean stopped;
    
    public void run() {
        while(!stopped) {
            //do some work
        }
    }
    
    public void pleaseStop() {
        stopped = true;
    }
    

    Add volatile to stopped variable and it works fine – if any other thread modifies stopped variable via pleaseStop() method, you are guaranteed to see that change immediately in working thread’s while(!stopped) loop. BTW this is not a good way to interrupt a thread either, see: How to stop a thread that is running forever without any use and Stopping a specific java thread.

    AtomicInteger

    private AtomicInteger counter = new AtomicInteger();
    
    public int getNextUniqueIndex() {
      return counter.getAndIncrement();
    }
    

    The AtomicInteger class uses CAS (compare-and-swap) low-level CPU operations (no synchronization needed!) They allow you to modify a particular variable only if the present value is equal to something else (and is returned successfully). So when you execute getAndIncrement() it actually runs in a loop (simplified real implementation):

    int current;
    do {
      current = get();
    } while(!compareAndSet(current, current + 1));
    

    So basically: read; try to store incremented value; if not successful (the value is no longer equal to current), read and try again. The compareAndSet() is implemented in native code (assembly).

    volatile without synchronization

    private volatile int counter;
    
    public int getNextUniqueIndex() {
      return counter++; 
    }
    

    This code is not correct. It fixes the visibility issue (volatile makes sure other threads can see change made to counter) but still has a race condition. This has been explained multiple times: pre/post-incrementation is not atomic.

    The only side effect of volatile is “flushing” caches so that all other parties see the freshest version of the data. This is too strict in most situations; that is why volatile is not default.

    volatile without synchronization (2)

    volatile int i = 0;
    void incIBy5() {
      i += 5;
    }
    

    The same problem as above, but even worse because i is not private. The race condition is still present. Why is it a problem? If, say, two threads run this code simultaneously, the output might be + 5 or + 10. However, you are guaranteed to see the change.

    Multiple independent synchronized

    void incIBy5() {
      int temp;
      synchronized(i) { temp = i }
      synchronized(i) { i = temp + 5 }
    }
    

    Surprise, this code is incorrect as well. In fact, it is completely wrong. First of all you are synchronizing on i, which is about to be changed (moreover, i is a primitive, so I guess you are synchronizing on a temporary Integer created via autoboxing…) Completely flawed. You could also write:

    synchronized(new Object()) {
      //thread-safe, SRSLy?
    }
    

    No two threads can enter the same synchronized block with the same lock. In this case (and similarly in your code) the lock object changes upon every execution, so synchronized effectively has no effect.

    Even if you have used a final variable (or this) for synchronization, the code is still incorrect. Two threads can first read i to temp synchronously (having the same value locally in temp), then the first assigns a new value to i (say, from 1 to 6) and the other one does the same thing (from 1 to 6).

    The synchronization must span from reading to assigning a value. Your first synchronization has no effect (reading an int is atomic) and the second as well. In my opinion, these are the correct forms:

    void synchronized incIBy5() {
      i += 5 
    }
    
    void incIBy5() {
      synchronized(this) {
        i += 5 
      }
    }
    
    void incIBy5() {
      synchronized(this) {
        int temp = i;
        i = temp + 5;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: public class FooBar { public static volatile ConcurrentHashMap myConfigData =
import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class Main implements Runnable { private final CountDownLatch cdl1
Say I have the following code: private Integer number; private final Object numberLock =
When I try to compile the following code... struct MemPages { size_t size; volatile
http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Atomic-Builtins.html I believe that the following code increases the value of var atomically. volatile
I have following code int i; //gobal var Thread1: { ... i=some value; }
I am wondering at the difference between declaring a variable as volatile and always
if a long variable is declared as :- private volatile long counter = 0;
The java memory model mandates that writing a int is atomic: That is, if
Assume this code: static inline void inc(int64_t* atomic) { __asm__ __volatile__ ( lock incq

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.