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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:06:06+00:00 2026-05-11T21:06:06+00:00

Are java primitive integers (int) atomic at all, for that matter? Some experimentation with

  • 0

Are java primitive integers (int) atomic at all, for that matter? Some experimentation with two threads sharing an int seems to indicate that they are, but of course absence of evidence that they are not does not imply that they are.

Specifically, the test I ran was this:

public class IntSafeChecker {
    static int thing;
    static boolean keepWatching = true;

    // Watcher just looks for monotonically increasing values   
    static class Watcher extends Thread {
        public void run() {
            boolean hasBefore = false;
            int thingBefore = 0;

            while( keepWatching ) {
                // observe the shared int
                int thingNow = thing;
                // fake the 1st value to keep test happy
                if( hasBefore == false ) {
                    thingBefore = thingNow;
                    hasBefore = true;
                }
                // check for decreases (due to partially written values)
                if( thingNow < thingBefore ) {
                    System.err.println("MAJOR TROUBLE!");
                }
                thingBefore = thingNow;
            }
        }
    }

    // Modifier just counts the shared int up to 1 billion
    static class Modifier extends Thread {
        public void run() {
            int what = 0;
            for(int i = 0; i < 1000000000; ++i) {
                what += 1;
                thing = what;
            }
            // kill the watcher when done
            keepWatching = false;
        }
    }

    public static void main(String[] args) {
        Modifier m = new Modifier();
        Watcher w = new Watcher();
        m.start();
        w.start();
    }
}

(and that was only tried with java jre 1.6.0_07 on a 32bit windows PC)

Essentially, the Modifier writes a count sequence to the shared integer, while the Watcher checks that the observed values never decrease. On a machine where a 32 bit value had to be accessed as four separate bytes (or even two 16bit words), there would be a probability that Watcher would catch the shared integer in an inconsistent, half-updated state, and detect the value decreasing rather than increasing. This should work whether the (hypothetical) data bytes are collected/written LSB 1st or MSB 1st, but is only probablistic at best.

It would seem very probable given today’s wide data paths that a 32 bit value could be effectively atomic, even if the java spec doesn’t require it. In fact, with a 32 bit data bus it would seem that you might have to work harder to get atomic access to bytes than to 32 bit ints.

Googling on “java primitive thread safety” turns up loads of stuff on thread-safe classes and objects, but looking for the info on the primitives seems to be looking for the proverbial needle in a haystack.

  • 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-11T21:06:06+00:00Added an answer on May 11, 2026 at 9:06 pm

    All memory accesses in Java are atomic by default, with the exception of long and double (which may be atomic, but don’t have to be). It’s not put very clearly to be honest, but I believe that’s the implication.

    From section 17.4.3 of the JLS:

    Within a sequentially consistent
    execution, there is a total order over
    all individual actions (such as reads
    and writes) which is consistent with
    the order of the program, and each
    individual action is atomic and is
    immediately visible to every thread.

    and then in 17.7:

    Some implementations may find it
    convenient to divide a single write
    action on a 64-bit long or double
    value into two write actions on
    adjacent 32 bit values. For
    efficiency’s sake, this behavior is
    implementation specific; Java virtual
    machines are free to perform writes to
    long and double values atomically or
    in two parts.

    Note that atomicity is very different to volatility though.

    When one thread updates an integer to 5, it’s guaranteed that another thread won’t see 1 or 4 or any other in-between state, but without any explicit volatility or locking, the other thread could see 0 forever.

    With regard to working hard to get atomic access to bytes, you’re right: the VM may well have to try hard… but it does have to. From section 17.6 of the spec:

    Some processors do not provide the
    ability to write to a single byte. It
    would be illegal to implement byte
    array updates on such a processor by
    simply reading an entire word,
    updating the appropriate byte, and
    then writing the entire word back to
    memory. This problem is sometimes
    known as word tearing, and on
    processors that cannot easily update a
    single byte in isolation some other
    approach will be required.

    In other words, it’s up to the JVM to get it right.

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

Sidebar

Ask A Question

Stats

  • Questions 159k
  • Answers 159k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the master, create a new property: public new MyDerivedPage… May 12, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer Which servlet container are you using? The docs for getHeader(String)… May 12, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer Can't you have 2px left-margin instead of 1px on each… May 12, 2026 at 11:22 am

Related Questions

In C++, I enjoyed having access to a 64 bit unsigned integer, via unsigned
Mixing the use of primitive data types and their respective wrapper classes, in Java,
I want to convert a primitive to a string, and I tried: myInt.toString(); This
following is the code listed in MainClass.java. public class MainClass { public static void

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.