The root cause of Java thread synchronization problems

Source: Internet
Author: User

1, the introduction of the cache:

The computing task of a computer cannot be done by a processor, at least with the memory of the processor. Since computer storage devices and processors operate at several orders of magnitude, modern computer systems have to add a cache of read and write speeds as close as possible to the processor's speed as buffers between memory and processors: Copying the data needed for the operation into the cache, allowing the operation to be done quickly, When the operation is finished, it is synchronized back to memory from the cache so that the processor does not have to wait for slow memory to read and write. Cache-based storage interaction solves the contradiction between processor and memory speed, but it also brings a higher degree of complexity to the computer system, because it introduces a new problem: cache consistency. In multiprocessor systems, each processor has its own cache, and they share the same main memory, which can result in inconsistent buffer data when the operations of multiple processors involve the same chunk of main memory. Java is the way to communicate between threads through shared memory, so thread synchronization problems follow.

2, Reorder:

In order to make the operating unit within the processor as fully utilized as possible, the processor may perform a random order optimization of the input code, and the processor will reorganize the results of the sequence execution after the calculation, ensuring that the result is consistent with the sequential execution, but does not guarantee that the order of the individual statements in the program is consistent with the order in the input code , so if there is a calculation task that relies on the intermediate result of another computational task, its order is not guaranteed by the order of the Code. Similar to the processor's unordered execution optimizations, Java virtual machines have similar reordering in the immediate compiler. That is, in order to optimize the program performance the compiler and the processor reorder the sequence of instructions. If two operations access the same variable, and one of the two operations is a write operation, there is a data dependency between the two operations. JMM guarantees that the compiler and processor do not reorder the two operations in a single-threaded program, but data dependencies between different processors and between different threads are not considered by the compiler and processor.


This article is from the "Evan" blog, be sure to keep this source http://wenshengzhu.blog.51cto.com/5151285/1983328

The root cause of Java thread synchronization problems

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.