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

  • Home
  • SEARCH
  • 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 765879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:57:23+00:00 2026-05-14T16:57:23+00:00

If variables in Java are accessed from multiple threads, one must ensure that they

  • 0

If variables in Java are accessed from multiple threads, one must ensure that they are safely published. This usually means using synchronizedor volatile.

I have got the impression, that some of my colleagues do not take this issue seriously, since they “never heard of volatile before and their programs have worked for years”.

So my question is:

Can someone provide an example Java program/snippet, that reliably shows data visibility problems.

I think running a program and seeing the unexpected NPE or stale variable value would help more, than just theoretic explanations, that cannot be demonstrated.

Thanks a lot for your help!

Update: Just to emphasis the point again. I have read Java Concurreny in Practice and know examples that theoretically have visibility issues. What I am looking for is a way to actually demonstrate them. I am not sure, that this is actually possible, but maybe there is a jvm configuration or something similar that allows it.

  • 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-14T16:57:24+00:00Added an answer on May 14, 2026 at 4:57 pm

    By modifying the example here by removing operations I have come up with an example that consistently fails in my environment (the thread never stops running).

    // Java environment:
    // java version "1.6.0_0"
    // OpenJDK Runtime Environment (IcedTea6 1.6.1) (6b16-1.6.1-3ubuntu3)
    // OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)
    public class Test2 extends Thread {
        boolean keepRunning = true;
        public static void main(String[] args) throws InterruptedException {
            Test2 t = new Test2();
            t.start();
            Thread.sleep(1000);
            t.keepRunning = false;
            System.out.println(System.currentTimeMillis() + ": keepRunning is false");
        }
        public void run() {
            while (keepRunning) 
            {}
        }
    }
    

    Note that this type of problems are quite dependent on the compiler/runtime/system. In particular the compiler can determine to add instructions to read the variable from memory even if it is not volatile –so the code would work–, the vm and jit can optimize away the reads from memory and use only registers, and even the processor can reorder instructions –that would not affect this case, but in other multithreaded cases it can affect the perceived state from other threads if more than one variable are modified.

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

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Add the following line to your routing table: routes.MapRoute("RouteName", "Help/{Thing}/{OtherThing}",… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer No, you're not using them correctly. Take a look at… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer You shouldn't escape the single quotes: $sql = "INSERT INTO… May 16, 2026 at 12:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am aware that the purpose of volatile variables in Java is that writes
I'm new to Java, and I'd like to create some class variables that are
In C# or Java, even though we mark methods as private, they still can
The Java static compiler (javac) inlines some static final variables and brings the values
I'm sure this is incredibly common with as OOP centered as Java is. In
In the book Java Servlet Programming , there's an example servlet on page 54
In essence, I want to put a variable on the stack, that will be
I do most of my development in Java and C++ but recently had to
I packaging my java project with maven, using the M2 Plug in Eclipse I
Is there a way to change the default java debugger in NetBeans 6.8 ?

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.