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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:07:01+00:00 2026-06-17T09:07:01+00:00

Reading a book, and this code comes up: public class Test { private static

  • 0

Reading a book, and this code comes up:

public class Test {

    private static boolean ready = false;
    private static int number = 0;

    public static class ListenerThread extends Thread {

        public void run() {

            while(!ready) {
                Thread.yield();
            }
            System.out.println(number);

        }

    }

    public static void main (String[] args) {

        new ListenerThread().start();
        number = 10;
        ready = true;

    }

}

The main points I was surprised about were mentioned by the author relatively quickly.

  1. They said ListenerThread might never terminate. I thought about this for a few days (in the back of my head), and my only conclusion is that it might be cached by that ListenerThread. Is that true? Would making ready volatile solve the problem (since it shouldn’t cache it anymore)?

  2. They also said the program might print 0. I understand now that Java might reorder the instructions, so ready becomes true to another thread before number is changed. Is there any method (technique), besides putting those instructions in a synchronized blocks solve the problem (on a central lock value)? I was thinking maybe implement notify()/wait(), but I feel it would suffer the same consequences. What is the best way to avoid the problem?

Thank you!

EDIT:

I just feel that I’ve read through a lot of code, and few bothered to protect against reordering in multiple threads. How common is this?

  • 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-17T09:07:02+00:00Added an answer on June 17, 2026 at 9:07 am

    my only conclusion is that it might be cached by that ListenerThread. Is that true? Would making ready volatile solve the problem (since it shouldn’t cache it anymore)?

    Not only cached, but the JIT can inline the value on the basis that the thread never changes it. i.e. it becomes hard coded.

    Using volatile prevent such assumptions being made. It will force it to read a cache consistent copy each time.

    I understand now that Java might reorder the instructions,

    Not only Java, but the CPU can re-order instructions. The JIT is aware that the CPU can do this re-ordering and AFAIK it rarely needs to as it assumes the CPU will do a good job.

    BTW Accessing a volatile variable also prevent instruction re-ordering so making ready volatile solves both problems.

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

Sidebar

Related Questions

I am reading a book in which author used a code like this public
Im reading a WPF book and I see this code: private void bgw1_ProgressChanged(object sender,
Note: This code actually codes from a tutorial book I'm reading at the moment,
I am reading a book saying that in C++ you can't do this: int
i was reading the book Delphi Developer's Guide to OpenGL and this code should
I'm trying to mastery php by reading book, and i'm stuck on this code
I'm reading the book Java™ I/O, 2nd Edition and there's this code: try {
I'm reading Expert F# book and I found this code open System.Collections.Generic let divideIntoEquivalenceClasses
I'm reading this book on C# and .NET and I'm learning a bunch of
I am reading this book Beginning Java™ EE 6 Platform with GlassFish™ 3: From

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.