Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to demonstrate java multithreading visibility problems?

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.

like image 854
Joe23 Avatar asked May 07 '10 08:05

Joe23


People also ask

What are the issue might appear in multithreading in Java?

Unpredictable results− Multithreaded programs can sometimes lead to unpredictable results as they are essentially multiple parts of a program that are running at the same time. Complications for Porting Existing Code − A lot of testing is required for porting existing code in multithreading.

What is visibility problem in Java?

The problem with threads not seeing the latest value of a variable because it has not yet been written back to main memory by another thread, is called a "visibility" problem. The updates of one thread are not visible to other threads.


1 Answers

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.

like image 113
David Rodríguez - dribeas Avatar answered Sep 22 '22 10:09

David Rodríguez - dribeas