"Java Concurrency Programming Practical" reading notes 8--deadlock, performance and scalability, lock granularity lock decomposition Lock segmentation

Source: Internet
Author: User

10th chapter to avoid the danger of being active

10.1 Dead Lock

-10.1.1 Lock Sequence Deadlock

The simplest form of deadlock:

-10.1.2 Dynamic Lock Sequence deadlock

The following methods can be used to solve the problem:

-10.1.3 deadlock occurs between collaboration objects

-10.1.4 Open Call

If you do not need to hold a lock when calling a method, this call is called an open call.

-10.1.5 Resource Deadlock

Deadlocks can also occur when multiple threads are waiting on the same resource.

10.2 Avoidance and diagnosis of deadlocks

-10.2.1 Support for timed locks

Displays the use of the Timer Trylock feature in the Lock Class (see Chapter 13) instead of the built-in locking mechanism.

10.2 Analysis of deadlocks through thread dump information

10.3 Other Active Dangers

-10.3.1 Hunger

"Hunger" occurs when a thread cannot continue execution because it cannot access the resources it needs. The most common resource that causes starvation is the CPU clock cycle.

-10.3.2 Poor responsiveness

-10.3.3 Live Lock

Although this problem does not block the thread, it cannot continue because the thread will repeatedly perform the same operation and always fail. A live lock typically occurs in an application that processes a transactional message: If a message cannot be processed successfully, the message-handling mechanism rolls back the entire transaction and puts it again at the beginning of the queue. If the message processor has an error while processing a particular type of message and causes it to fail, a transaction rollback occurs whenever the message is fetched from the queue and passed to the processor that has the error. Since this message is then placed at the beginning of the queue, the processor is called repeatedly and returns the same result. Although the thread that handles the message is not blocked, it cannot continue. To solve this problem, you need to introduce randomness into the retry mechanism.

11th Chapter Performance and Scalability

11.1. Thinking about Performance

11.1.1 Performance and Scalability

11.1.2 evaluation of various performance tradeoff factors

11.2 Amdahl Law

In the case of increasing the computer resources, the program can theoretically achieve the highest speedup ratio, which depends on the proportion of the parallel component and the serial component. Assuming that F is a part that must be executed serially, then according to Amdahl's law, the highest speedup ratio in a machine containing n processors is:

To predict how much speedup an application will achieve in a multiprocessor system, you also need to identify the serial part of the task.

11.2.1 Example: A serial part that is hidden in various frames

11.3 Thread-introduced overhead

11.3.1 Context Switch

The cost of the JVM and the operating system. In addition, when a new thread is switched in, the data it needs may not be in the local cache of the current processor, so the context switch will cause some caches to be missing, so that the thread will run more slowly on the first dispatch.

11.3.2 Memory Synchronization

Memory Fence (Barrier). The memory fence refreshes the cache, invalidates the cache, refreshes the write buffer of the hardware, and stops execution of the pipeline. Modern JVMs can be optimized to remove some locks that do not compete, thereby reducing unnecessary synchronization overhead. Some more complete JVMs can use escape analysis to find local object references that are not published to the heap. Even without an escape analysis, the compiler can perform lock granularity coarsening (lock coarsening) to merge adjacent synchronization blocks with a single lock.

11.3.3 Blocking

When the JVM implements blocking behavior, it can take a spin wait (through a loop that tries to acquire the lock continuously, know the success) or suspend the blocked thread through the operating system.

11.4 Reduction of lock holding time

11.4.1 narrowing the range of locks

Here's an example:

Because there is only one state variable attributes in Attributestore, it is possible to further improve its performance by delegating thread security to other classes. By using a thread-safe map (hashtable,synchronousmap or concurrenthashmap) instead of attributes, Attributestore can delegate the task of ensuring thread safety to the top-level thread-safe container.

11.4.2 reducing the granularity of locks

Reduce the frequency of thread request lock, can be achieved by the techniques of lock decomposition and lock segmentation. Separate locks are used to protect independent state variables. While reducing the granularity of lock operations and achieving greater scalability, the more locks you use, the greater the risk of lock-and-death locks. If a lock is to protect multiple independent variables, the lock can be decomposed into multiple locks, and each lock will only protect one variable, thereby improving scalability. Here's an example:

Change the code above without serverstatus locks to protect the user state and check the blood state, but each state is protected by a lock, as shown in the following program:

11.4.3 Lock Segment

In some cases, the lock decomposition technique can be further extended to the lock decomposition on a set of independent objects, which is known as lock fragmentation.

(Looks like a good look). One disadvantage of lock segmentation is that obtaining multiple locks for exclusive access is more difficult and expensive than using a single lock for exclusive access.

11.4.4 Avoid hotspot domains

Both lock decomposition and lock segmentation enable different threads to operate on different data (or different parts of the same data) without interfering with each other. If the program uses lock segmentation, it is important to show that the competition frequency of the lock is higher than the frequency at which the locks are protected.

When each operation requests multiple variables, the granularity of the lock is difficult to reduce. This is another aspect of mutual checks and balances between performance and scalability, and some common optimizations, such as caching some of the repeated calculations, introduce "hotspot domains" that tend to limit scalability.

11.4.5 some alternatives to exclusive locks

The third technique for reducing the impact of a competitive lock is to abandon exclusive locks, which helps to manage shared state in a friendly and concurrent manner. For example, use concurrent containers, read-write locks, immutable objects, and atomic variables.

11.4.6 Detection of CPU utilization

11.4.7 say "No" to the object pool

11.5 Example: Comparing the performance of a map

In a single-threaded environment, the performance of Concurrenthashmap is slightly better than the performance of synchronous HashMap, but it is much better in a concurrent environment. In the implementation of CONCURRENTHASHMAP, it is assumed that most commonly used operations are to obtain a value that already exists, so it optimizes various get operations to provide the highest performance and concurrency. In the implementation of a synchronized map, the main obstacle to scalability is that there is only one lock in the entire map, so only one thread can access the map at a time. The difference is that CONCURRENTHASHMAP does not lock most read operations and uses the lock segmentation technique for write operations and other read operations that require locks.

11.6 Reducing the overhead of context switching

When a task transitions between running and blocking states, it is the equivalent of an up and down switch. In a server application, one of the reasons for blocking occurs is the various log messages that are generated when the request is processed. To illustrate how to improve throughput by reducing the number of context switches, we will analyze the scheduling of the two log methods. (This part is not clear in the analysis of what ....) Sweat.... Pass off first)

"Java Concurrency Programming Practical" reading notes 8--deadlock, performance and scalability, lock granularity lock decomposition Lock segmentation

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.