Java Multithreaded Understanding 2

Source: Internet
Author: User

1. When do I have to synchronize? What does synchronization mean? How do I sync?

To maintain the correct visibility across threads, as long as you share non-final variables between several threads, you must use the

Synchronized (or volatile) to ensure that one thread can see changes made by another thread.

Synchronization is necessary for reliable communication between threads and for mutually exclusive access. This is due to the Java language Specification of memory

Model

Summary: To prevent multiple threads from concurrently modifying the same data, synchronization is required, otherwise inconsistent data is created

2, what is called Atomic (atomic operation)?

A Java atomic operation is an operation that is not interrupted. (Is mutual exclusion and visibility?!) )

Is it true that atomic manipulation can actually achieve the thread-safe synchronization effect? There are actually some atomic operations that are not necessarily thread safe

4, Java synchronization Mechanism has 4 ways to achieve: (partial reference to online resources)

①threadlocal②synchronized () ③wait () and notify () ④volatile

Purpose: To solve the access violation of the same variable in multi-threading

3. Don't confuse: synchronous, asynchronous

For example: Ordinary B/s mode (synchronous) Ajax Technology (asynchronous)

Synchronize: Submit request, wait for server processing, and then return to this period the client browser cannot do anything

Asynchronous: Requests are processed through event triggering server processing (which is still something the browser can do)

Visible, he "sync" is not this "sync"--we say that the shared data synchronization in Java (synchronized)

A synchronized object is a behavior (action), and a synchronized object refers to a substance (shared data).

Of

So, under what circumstances are atomic operations not thread-safe? This may be the cause: Java threads allow threads to

Save a copy of the variable in its own memory area. Allows a thread to work with a local private copy rather than using the main memory value every time

To improve performance (I humble opinion: Although the atomic operation is thread-safe, each thread can get the variable (read operation), the

Self-toying with its own copy, the update operation (write operation) because it is not written in the primary, causing other threads not to be visible.

So how do we fix it? Therefore, the Java synchronization mechanism is required.

Java Multithreaded Understanding 2

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.