Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda