Is the != check thread safe?

后端 未结 8 1842
醉酒成梦
醉酒成梦 2021-01-29 18:56

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 t

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-29 19:40

    Proved with test-ng:

    public class MyTest {
    
      private static Integer count=1;
    
      @Test(threadPoolSize = 1000, invocationCount=10000)
      public void test(){
        count = new Integer(new Random().nextInt());
        Assert.assertFalse(count != count);
      }
    
    }
    

    I have 2 fails on 10 000 invocations. So NO, it is NOT thread safe

提交回复
热议问题