Are primitive datatypes thread-safe in Java

前端 未结 4 781
生来不讨喜
生来不讨喜 2020-11-28 13:38

Are the primitive data types like int & short thread-safe in Java? I have executed the following code and couldn\'t see expected result 500 som

4条回答
  •  执念已碎
    2020-11-28 13:40

    I would suggest using classes in java.util.concurrent.atomic. They are designed for thread-safety and in some cases the JVM can take advantage of hardware features to optimize.

提交回复
热议问题