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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:20:16+00:00 2026-05-28T14:20:16+00:00

Java Concurrency in Practice gives the following example of an unsafe class which due

  • 0

“Java Concurrency in Practice” gives the following example of an unsafe class which due the nature of the java memory model may end up running forever or print 0.

The issue this class is trying to demonstrate is that the variables here are not “shared” between threads. So the value on thread sees can be different from another thread as they are not volatile or synchronized. Also due to the reordering of statements allowed by the JVM ready=true maybe set before number=42.

For me this class always works fine using JVM 1.6. Any idea on how to get this class to perform the incorrect behavior (i.e. print 0 or run forever)?

public class NoVisibility {
    private static boolean ready;
    private static int number;

    private static class ReaderThread extends Thread {
        public void run() {
            while (!ready)
                Thread.yield();
            System.out.println(number);
        }
    }

    public static void main(String[] args) {
        new ReaderThread().start();
        number = 42;
        ready = true;
    }
}
  • 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-28T14:20:17+00:00Added an answer on May 28, 2026 at 2:20 pm

    The problem you have is that you are not waiting long enough for the code to be optimised and the value to be cached.

    When a thread on an x86_64 system reads a value for the first time, it gets a thread safe copy. Its only later changes it can fail to see. This may not be the case on other CPUs.

    If you try this you can see that each thread is stuck with its local value.

    public class RequiresVolatileMain {
        static volatile boolean value;
    
        public static void main(String... args) {
            new Thread(new MyRunnable(true), "Sets true").start();
            new Thread(new MyRunnable(false), "Sets false").start();
        }
    
        private static class MyRunnable implements Runnable {
            private final boolean target;
    
            private MyRunnable(boolean target) {
                this.target = target;
            }
    
            @Override
            public void run() {
                int count = 0;
                boolean logged = false;
                while (true) {
                    if (value != target) {
                        value = target;
                        count = 0;
                        if (!logged)
                            System.out.println(Thread.currentThread().getName() + ": reset value=" + value);
                    } else if (++count % 1000000000 == 0) {
                        System.out.println(Thread.currentThread().getName() + ": value=" + value + " target=" + target);
                        logged = true;
                    }
                }
            }
        }
    }
    

    prints the following showing its fliping the value, but gets stuck.

    Sets true: reset value=true
    Sets false: reset value=false
    ...
    Sets true: reset value=true
    Sets false: reset value=false
    Sets true: value=false target=true
    Sets false: value=true target=false
    ....
    Sets true: value=false target=true
    Sets false: value=true target=false
    

    If I add -XX:+PrintCompilation this switch happens about the time you see

    1705    1 % RequiresVolatileMain$MyRunnable::run @ -2 (129 bytes)   made not entrant
    1705    2 % RequiresVolatileMain$MyRunnable::run @ 4 (129 bytes)
    

    Which suggests the code has been compiled to native is a way which is not thread safe.

    if you make the value volatile you see it flipping the value endlessly (or until I got bored)

    EDIT: What this test does is; when it detect the value is not that threads target value, it set the value. ie. thread 0 sets to true and thread 1 sets to false When the two threads are sharing the field properly they see each others changes and the value constantly flips between true and false.

    Without volatile this fails and each thread only sees its own value, so they both both changing the value and thread 0 see true and thread 1 sees false for the same field.

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

Sidebar

Related Questions

I am reading Java Concurrency in practice and looking at the example code on
I am reading Java Concurrency in practice and looking at the example code on
I am reading this book called Java Concurrency in Practice and the author gives
I'm studying Java Concurrency in Practice and there it is explained why the following
In Brian Goetz's book, Java Concurrency in Practice, his example of a Reentrant lock
This is the implementation of the BoundedExecutor class in the Java Concurrency in Practice
Possible Duplicate: Thread safety in Java class I'm reading Java concurrency in Practice ,
Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
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

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.