Third to break the Java myth: atomic operation is a thread safe

zhaozj2021-02-16  74

Tensone operation in Java is often mentioned by thread safety. Depending on the definition, the atomic operation is not interrupted, so it is considered to be threaded. In fact, some atomic operations do not necessarily have threads. The reason for this problem is to minimize keywords in the code. Synchronization can damage performance, although this loss is different due to JVM. In addition, in modern JVM, the performance of synchronization is gradually improved. Despite this, use synchronization still has performance costs, and programmers will always try to improve their code efficiency, so this issue continues. In Java, the assignment of 32-bit or less bits is atom. On a 32-bit hardware platform, other original types of Double and long are often represented using 32 bits, while Double and long are usually represented by 64 bits. In addition, the object reference is implemented using this unit pointer, usually 32 bits. The operation of these 32-bit types is atomic. These original types typically use 32-bit or 64 bits, which in turn introduces another little myth: the size of the original type is guaranteed by language. this is not right. The Java language guarantees the ranging range of the original type and the storage size in the JVM. Therefore, the INT type always has the same range range. 32-bit implementations may be used on one JVM, and may be 64 bits on another JVM. This is again emphasized: It is guaranteed on all platforms that the number range, 32-bit, and smaller values ​​are atoms. So, where is the atomic operation? Isn't the thread safe? The main point is that they may really be threaded, but this is not guaranteed! The Java thread allows the thread to save the variable of the variable in its own memory area. Allowing threads to use local private copies to work rather than each time you use the value of the main memory is to improve performance. Consider the following class: Class RealTimeClock {Private Int CLKID; Public Int ClockId () {Return CLKID;} PUBLIC VOID SETCLOCKID (INT ID) {CLKID = ID;} // ...

转载请注明原文地址:https://www.9cbs.com/read-13099.html

New Post(0)