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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:54:55+00:00 2026-06-13T04:54:55+00:00

The java concurrency tutorial states that: Reads and writes are atomic for reference variables

  • 0

The java concurrency tutorial states that:

Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
Reads and writes are atomic for all variables declared volatile (including long and double variables).

I created a simple experiment to test this

package experiment0;

public class Experiment0 {

    public static int i=9;

    public static void main(String[] args) {
        i=9;
        (new Thread(){
            public void run(){
                while(i==9){
                    //System.out.println("i==9");
                }
                System.out.println("\ni!=9");
            }
        }).start();
        (new Thread(){
            public void run(){
                try{
                    Thread.sleep(3000);
                    i=8;
                }catch(Exception e){

                }
            }
        }).start();
    }
}

When I run this code the program never gets terminated! In order for it to work I either have to uncomment the line inside the while loop of the first thread or declare i as volatile.
Am I missing something? Is the documentation wrong and should it say that all primitives should be declared as volatiles in these kind of cases? Why does a write to System.out “solve” the problem? Does it give enough time to the second thread to change i?

  • 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-13T04:54:56+00:00Added an answer on June 13, 2026 at 4:54 am

    A write being atomic means that the write is executed as a single, atomic operation. No other thread could see something other than the value before the assignment, or after the assignment.

    For example, in the case of long values, an assignment is not atomic because it’s actualy executed as two operations: one which assigns the first 32 bits, and one which assigns the last 32 bits. So another thread might read the variable’s value after only one of the two operations has been executed, ans thus see a value that is neither the before nor the after value, but an “in-between” value.

    What your program demonstrates is that writing to a variable without any kind of synchronization causes visibility problems between threads. The write done by one thread is not visible by another thread. This is due to processors each having their own cache, and not writing to the main memory at each assignment. To make sure a write is visible, you need to make the variable volatile, or to synchronize access to the variable, or to use an AtomicInteger.

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

Sidebar

Related Questions

I have read Java Concurrency in Practice and this is a great reference, but
Goetz's Java Concurrency in Practice , page 41, mentions how this reference can escape
From book Java Concurrency in Practice page 26: You can use volatile variables only
I am working on a tutorial for my Java concurrency course. The objective is
I'm designing a concurrent Java application that reads data from various medical devices available
This is a modification of code from the Java tutorial on Concurrency package threads;
Oracle's documentation on atomic access (at http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html ) says this: a volatile variable establishes
According to Brian Goetz's Java Concurrency in Practice JVM can't exit until all the
Just what the title states; the term appeared in 'Java Concurrency in Practice'. I
In Java Concurrency in Practice there is a sample that made me confused: public

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.