Is the != check thread safe?

atomic, java, multithreading, race-condition, thread-safety

Solution

In the absence of synchronization this code

Object a;

public boolean test() {
    return a != a;
}

may produce `true`. This is the bytecode for `test()`

    ALOAD 0
    GETFIELD test/Test1.a : Ljava/lang/Object;
    ALOAD 0
    GETFIELD test/Test1.a : Ljava/lang/Object;
    IF_ACMPEQ L1
...

as we can see it loads field `a` to local vars twice, it's a non-atomic operation, if `a` was changed in between by another thread comparison may produce `false`.

Also, memory visibility problem is relevant here, there is no guarantee that changes to `a` made by another thread will be visible to the current thread.

Problem

I know that compound operations such as `i++` are not thread safe as they involve multiple operations. But is checking the reference with itself a thread safe operation? ``` a != a //is this thread-safe ``` I tried to program this and use multiple threads but it didn't fail. I guess I could not simulate race on my machine. EDIT: ``` public class TestThreadSafety { private Object a = new Object(); public static void main(String[] args) { final TestThreadSafety instance = new TestThreadSafety(); Thread testingReferenceThread = new Thread(new Runnable() { @Override public void run() { long countOfIterations = 0L; while(true){ boolean flag = instance.a != instance.a; if(flag) System.out.println(countOfIterations + ":" + flag); countOfIterations++; } } }); Thread updatingReferenceThread = new Thread(new Runnable() { @Override public void run() { while(true){ instance.a = new Object(); } } }); testingReferenceThread.start(); updatingReferenceThread.start(); } } ``` This is the program that I am using to test the thread-safety. Weird behavior As my program starts between some iterations I get the output flag value, which means that the reference `!=` check fails on the same reference. BUT after some iterations the output becomes constant value `false` and then executing the program for a long long time does not generate a single `true` output. As the output suggests after some n (not fixed) iterations the output seems to be constant value and does not change. Output: For some iterations: ``` 1494:true 1495:true 1496:true 19970:true 19972:true 19974:true //after this there is not a single instance when the condition becomes true ```

Original source

Related problems