Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Visibility of mutable instance objects

As per the book “Java concurrency in practice”, the below code might execute forever or value of number may still be 0 when ready is true and the recommendation was to define the variables as volatile. However the below listed program always seems to return the correct value (42) instead of stale value even after I tried this multiple times. Is this a phenomenon that occurs very rarely?

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

    private static class ReaderThread extends Thread{
        public void run(){
            System.out.println("Thread started =" + ready + " " + number);
            while(!ready){
                Thread.yield();
            }
            System.out.println("value is" + number);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new ReaderThread().start();
        Thread.sleep(10000);
        number = 42;
        ready = true;
    }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Is this a phenomenon that occurs very rarely

Yes, rare. However, given computers that do things billions of times per second, rare is quite common.

Whenever accessing resources across threads, you must address thread-safety. Otherwise, your app in deployment will suffer sporadic bugs that are terribly difficult if not impossible to solve.

By this same logic, testing for concurrency bugs is notoriously difficult, as you have seen in your Question.

Always look to avoid threaded code where possible. For example, utilize immutable objects or defensive copies.

Required reading: Java Concurrency in Practice by Brian Goetz, et al.

over 4 years ago · Santiago Trujillo Report

0

Author says server JVM performs more optimisation then the client JVM. a paragraph fromt he book

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!