Java Virtual machine Concurrent Programming Learning notes

Source: Internet
Author: User
Tags volatile

The book "Java Virtual machine concurrency programming" is really brief encounter. I've only scratched the surface of concurrent programming, and this book gives me a whole new understanding of concurrent programming. So put the knowledge points in the book to take notes, in order to review the use later.

Concurrency and parallelism

Carefully speaking, concurrency and parallelism are two different concepts. But with the popularity of multicore processors, different threads of concurrent programs tend to be assigned to different processor cores by compilers, so concurrent programming and parallelism are the same for upper-level programmers.

Concurrent risk of starvation

When a thread waits for a time that needs to run for a long time or is never completed, the thread is starved. Often hunger is also called a live lock.

The way to solve hunger is to design a wait time-out policy that allows threads to wait for a limited

Dead lock

Two or more threads wait for each other to release a resource or perform certain actions.

Competitive conditions

Competitive conditions refer to two threads competing to use the same resources or data.

The main reasons for competitive conditions are: just-in-time (JIT) compiler optimizations and Java memory models.

Memory Fence

Memory Fence (memory Barrier) refers to a copy action from a local cache (or register) or working memory to main storage. When the program is running, all changes are made to the register or the local cache before being copied to main memory to span the ram fence. This cross-order is called Happens-before.

The write operation must happens-before the read operation, that is, the write operation needs to complete its own spanning action before all the read threads cross the memory fence, and its changes can be made visible to other threads.

Many operations in the Java Concurrency API imply the implication of crossing the memory fence: volatile, sychronized, start () in Thread, and interrupt (), Functions in Executorservice and synchronization tools such as to Countdown.

Volidate

The role of the keyword volidate is to tell the JIT compiler not to perform any optimizations that might affect the order of the tag variables, and read and write access to the variable requires ignoring the local cache and manipulating the memory directly. However, each change access is made to cross the memory fence and eventually degrade the program performance. Also, in scenarios where multiple fields are accessed concurrently by multiple threads, the volatile keyword cannot guarantee the atomicity of the overall operation because the access to each volidate field is handled independently and cannot be uniformly coordinated into one access.

The workaround is to mask direct access to the variable and all access is directed to the getter and setter methods through synchronization.

Java Virtual machine Concurrent Programming Learning notes

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.