[Reading Notes] Practical Java High Concurrency Programming (Second Edition) Reading Notes


Insert picture description here

1 Overview

After reading this book, take a look at the implementation logic of meter

1.1. Walk into the parallel world

Forget about parallelism and Moore's Law
. The difference between concurrency and parallelism is good.
Multi-threaded deadlock,
livelock amdahl's law
32-bit long data read is not atomic.
Instruction rearrangement and the happen-before principle.

1.2 Basics of Java Parallel Programs

Mainly explained thread-related content, as well as some important keywords, you can take a look.

1.3 jdk concurrent package

This chapter goes from the shallower to the deeper, the explanation is very good, gradually in-depth, very suitable for beginners and the second time to watch

Synchronous control

  1. Explain the use of synchronized keyword
  2. A good partner for reentrant locks, the condition is very similar to the wait of the object
  3. Explained the semaphore, which is equivalent to the limit on the number of simultaneous entry into the critical section
  4. Explained the separation idea of ​​read-write lock and performance test
  5. Countdown counter, rocket launch program
  6. Circulation fence army team assembly case
  7. Use of on-site blocking tool lockSupport
  8. guava's current limit RateLimiter

Thread pool explanation

  1. What is thread pool
  2. Introduction to the existing thread pool of jdk and its disadvantages
  3. The thread pool internally implements ThreadLocalPool
  4. Introduction to rejection strategies and custom rejection strategies
  5. Extend the thread pool, the AOP after the execution of the method before
  6. Thread pool size setting formula
  7. Custom thread pool to print on-site stack information (this is very important)
  8. fork join框架 ForkJoinPool
  9. Extension of thread pool in guava

jdk concurrent container

  1. hashmap list currentHashMap CopyOnWriteArrayLisy
  2. BlockingQueue
  3. Jump table for data order (useful)

JMH performance test

  1. Tool introduction and use

1.4 Lock optimization and precautions

some advices

  1. Reduce lock holding time
  2. Reduce lock granularity currentHashMap segmented lock
  3. Use separate read-write locks instead of exclusive locks
  4. Lock separation, ArrayBlockingQueue take() method is to take data from the head, and put() method is to put data from the tail, these two do not interfere with each other, and one lock should not be used.
  5. Lock coarsening lifts the lock in the for loop outside the for loop

Efforts made by the java virtual machine on lock optimization

  1. Partial lock, the first thread to acquire the lock, the next request is still to acquire the lock, no synchronization, when the lock competition is fierce, the effect is not good
  2. Lightweight locks use the object head as a pointer to point to the stack of the thread holding the lock to determine whether a thread holds the object lock. If the lock fails, it will expand into a heavyweight lock.
  3. Spin lock, the last effort of the virtual machine, empty loop for several cycles, in case the thread acquires the lock by itself
  4. Lock elimination, locks introduced intentionally or unintentionally, the virtual machine based on escape analysis, if it is safe, then the lock will be eliminated

threadLocal introduction

  1. threadLocalMap will be cleaned up when the thread exits, it is recommended to actively set null to prevent memory leaks

no lock

  1. cas loop, once a conflict is detected, the current operation is retried until there is no conflict
  2. There is no lock-free system overhead caused by lock competition, and no overhead caused by ordinary scheduling between threads
  3. Most modern processors support atomic CAS instructions
  4. AtomicInteger without lock
  5. A brief introduction to the pointer Unsafe class in java, but no in-depth explanation
  6. The lock-free reference object, AtomicReference to the ordinary corresponding reference, can ensure that the thread safety when you modify the object reference has the problem of ABA
  7. The object reference with time stamp AtomicStampedReference solves the problem that the object loses its state during the modification process.
  8. Array can also be lock-free, AtomicIntegerArray
  9. Let ordinary variables also enjoy atomic operations. AtomicIntegerFieldUpdater This is a good one, but it is not commonly used. I browsed it and didn’t take a closer look.
  10. Threads help each other, the Synchronous capacity is 0, and any write to Synchronous needs to wait for a read operation to Synchronous. The follow-up explains the source code related, more in-depth, you can take a look.

Questions about deadlock

  1. The philosopher’s meal problem without depth

1.5 Parallel mode and algorithm

Singleton mode

Invariant mode

  1. java String type

Producer consumer model

Lock-free cache framework disruptor

  1. I didn’t look closely at a case

cpu cache optimization to solve the problem of false sharing

  1. A bunch of longs occupies 64 bytes, which happens to be a data block. I have blogged about this

feature mode

  1. Custom feature mode

The following is the sorting algorithm, not much to look at

Network NIO
nio related cases, not much to look at, too much code

1.6 java 8 9 10 and concurrency

Function as a first-class citizen

Fundamentals of functional programming

  1. FunctionalInterface annotation
  2. lambda expression

Enhanced Future: CompletableFuture

  1. CompletableFuture implements the Futureje interface and also the CompletionStage interface (more than 40 methods)
  2. Support timeout

Improved read-write lock StampedLock

  1. Although the read-write lock separates the function of read harmony, the read lock will completely block the write lock.
  2. The internal implementation of StampedLock is based on the CLH lock. The CLH lock is a spin lock, which guarantees that no starvation occurs and the FIFO service order can be guaranteed.
  3. The basic idea: The lock maintains a queue of waiting threads. All threads that apply for locks but have not succeeded are recorded in this queue. Each node (a node represents a thread) saves a mark as (locked) to determine whether the current thread has been released Up the lock.
  4. I didn’t take a closer look at this point about the principle explanation

Atomic enhancement

  1. Faster atomic class LongAdder
  2. Enhancement of currentHashMap, map.compyteIfAbsent

Publish and subscribe model

  1. Java 9 reactive programming reflects that social programming is used to process data in asynchronous streams. Whenever the application receives a data item, it will process it. Reactive programming processes data in the form of a stream, so it is more efficient to use in memory high. Because it is a simple look at jdk 9, I didn't understand it, because jdk8 will continue for a long time.

1.7 Use akka to build highly concurrent programs

Because I learned netty, what akka can do, so can netty, what netty can do, but akka can’t do it, so I skipped this chapter directly, because I didn't have the opportunity to use it, just concentrate on netty.

1.8 Parallel program debugging

There is nothing good in this chapter. It mainly explains the debugging of eclipse, which has nothing to do with IDEA, and eclipse is not needed for a long time because it is skipped. I still know a little bit about debugging.

And there is a lot of content in this chapter, it doesn't hurt to read it, I just browsed it.

1.9 Jetty core code analysis

I didn't watch it because I didn't use it

Guess you like

Origin blog.csdn.net/qq_21383435/article/details/114693378