Java summary multithreaded (a)

Recent reading "Java Concurrency in combat," the book, the author also java current contract and the author wrote this book, I also through a course of time geeks know this book, then immediately buy orders this book, I say the focus here, and I have read through the curriculum and their own following receipt, we hope to help you.

This week I share the following four main points of knowledge:

  • From the perspective of macro and micro concurrent programming knowledge, we got to see nature, panoramic view out of this learning method
  • Concurrent Java programming change reasons: visibility, atomicity, ordering
  • Locks and the object lock; fair and unfair latch lock; reentrant lock; fine-grained and coarse-grained locks lock
  • How to avoid deadlocks

study method

Here mainly want to share with you is to study a problem, we must seize the core knowledge, this knowledge is the first study to do, and the second to know the advantages and disadvantages of this knowledge in order to know what they should on their own applied to him, and his knowledge integration scenario, you want to see the third principle to achieve, because if applied to the project, you have to know, know why, in order to truly understand this knowledge, so that we concurrent programming this knowledge as a case to analyze:

  1. This knowledge is doing?

    This knowledge is used to solve the problem of efficient performance optimization, like on the Foolish Old Man, he was a man to move a mountain lifetime else fails, just like a single thread to do the time-consuming task, but if you use multiple threads to solve this problem, the whole mountain is likely to move those, more than enough, then if the thread, multi-thread performance is mainly the efficient use of multi-core CPU computer.

  2. pros and cons

    Make full use of the advantages is that computer performance can be multiple tasks in parallel, there is no drawback if used properly, prone to concurrency issues, such as thread waits, deadlocks and so on.

  3. Realization of the principle

The main thread is achieved by the tube to achieve, inside the Java sycnized and lock condition is achieved, the thread is controlled by switching notify wait notifyall

Source of concurrent programming Bug: visibility, atomicity, ordering

Concurrent programming problem arises, how these issues have caused it? If a single-threaded process a transaction, they will not have problems, such as local variables will exist for stack space, there Threadlocal we usually use, he is by Map identifies each thread is stored, is equivalent to a single ring threading issues, so it will not be complicated by problems.

We concurrency issues arise, mainly because we are now mostly multi-core CPU, each task is a thread processing, and the operating system has a notion of processes and threads, a process can hold multiple threads, thread creation is on the basis of the replication process, a reference operation plus 1, after we deal with multi-threading, by the time slice allocated to the CPU processing tasks.

Because the CPU processing day, year-memory processing, memory handling one day, IO processing decade, under this approach hanging difference, according to the bucket theory, the board look at the shortest restricts how much water can be loaded, the operating system increases the CPU caching, compiler optimization to solve the synchronization problem, Java memory model, Happen-Before principles.

Because the CPU cache to perform thread switching cause visibility problems, a number of high-level language instruction CPU instruction, when the switching process in the thread will appear atomic issues, as well as the wording of DCL if not volitate modifier, then allocate memory, assignment order to create an object will change, it will cause a null pointer, this will result in the orderliness of the problem.

Lock to use in practice of

We usually asked in the interview common is the following: class lock and the object lock; lock fair and unfair lock; reentrant lock; fine-grained and coarse-grained lock lock, then we will explain everything these problem

Locks and lock objects

The lock is clas file structure or class static method is lock, if the lock is this, member variables, non-static method is the object lock, both lock is not mutually exclusive of a range of no effect

Fair and unfair lock lock

If A, B, C three threads compete a lock, if thread A has been holding the lock, the lock is released if the thread A, B, C are likely to get the lock, but in some cases, B than C wait a long time, if it is fair lock then, B will be priority

Fine-grained and coarse-grained lock lock

Examples of the above side is still, a business scenarios described herein, for locking in the transfer operation requires two addMoney and methods reduceMoney, if the lock object directly, only two serial operation, but if the two operations were used own lock, lock1 lock2 locks and lock, so that you can at the same time, speed up the efficiency.

Deadlock

Concurrency is advanced knowledge in any one point, the concurrent if not handled properly it will have concurrency problems if the above operation transfer operation, fine-grained locking improve performance, but the problem here deadlock occurs, if the thread T1 to user A B transfers, Tl holds the lock object a, then B to transfer the thread T2 a, T2 B holds the lock object, the object B is waiting for the lock Tl, T2 waits for the lock object a, thereby forming a deadlock, where "Java concurrency in combat" inside three three principles to promote the use of concurrent avoid deadlocks.

  1. Priority concurrent use sophisticated tools
  2. No alternative but to use low-level synchronization original language
  3. After avoiding premature optimization, there is a bottleneck in optimization

Quote

  1. Geek Time "Java Concurrency in combat" Author: Vang Pao order
  2. "Java Concurrency in combat" Author: Doug Lea

Guess you like

Origin juejin.im/post/5ddaa7ab6fb9a07ab75bc9f9