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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:35:32+00:00 2026-05-28T06:35:32+00:00

In Java Concurrency in Practice there is a sample that made me confused: public

  • 0

In Java Concurrency in Practice there is a sample that made me confused:

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

    private static class ReaderThread implements Runnable {

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

    public static void main(String[] args) {
        System.out.println("0");
        new Thread(new ReaderThread()).run();
        System.out.println("1");
        number = 42;
        System.out.println("2");
        ready = true;
        System.out.println("3");
    }
}

I can understand reordering makes the loop to never break, but I can’t understand why “1”, “2” and “3” are never printed to console. Could any body help?

  • 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-28T06:35:33+00:00Added an answer on May 28, 2026 at 6:35 am

    You don’t spawn a new thread but run it in the current one. Use the start() method instead.

    Since you run() executes on the main thread and that method runs in an endless loop you’ll never reach the System.out.println() statements (and neither do you reach ready = true;).

    From the JavaDoc on run():

    If this thread was constructed using a separate Runnable run object, then that Runnable object’s run method is called; otherwise, this method does nothing and returns.

    And start():

    Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

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

Sidebar

Related Questions

Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
Java Concurrency in Practice gives the following example of an unsafe class which due
I'm studying Java Concurrency in Practice and there it is explained why the following
I am reading Java Concurrency in Practice and kind of confused with the thread
I am reading Java concurrency in practice and the below examples are from that.
This is the implementation of the BoundedExecutor class in the Java Concurrency in Practice
In Java Concurrency In Practice book (p.156), there's a statement regarding poison pill approach:
In the famous Java Concurrency in Practice, section 2.4, it says that intrinsic locking
Possible Duplicate: Thread safety in Java class I'm reading Java concurrency in Practice ,
I have read Java Concurrency in Practice and this is a great reference, but

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.