Discussion on Java thread concurrency knowledge

  • 2020-05-24 05:40:08
  • OfStack

An object is an object that can be referenced by code outside its current scope:

Common forms: storing a reference to an object in a public static field; Returns a reference in a non-private method; Publish inner class instances, including references.

Escape: to publish an object before it is ready.

Do not let the this reference escape from the constructor. For example, start the thread in the constructor and the thread will contain a reference to the object.

Synchronous container: cross-access to all state of the container, Vector, Hashtable, Cllections.synchronizedMap |List

Concurrent containers: ConcurrentHashMap, CopyOnWriteArrayList, ConcurrentLinkedQueue, BlockingQueue

Advantages of the list random access feature.

Blocking adds a blocking get set operation

ConcurrentHashMap: separates locks for high throughput for concurrent access with little loss of single thread access performance. Returns the iterator with weak 1 uniqueness.

Weak 1 tropism of the iterator, which detects changes in the container after the iterator is generated.

The concurrency container size(), isEmpty() is weakened and returns approximate results.

CopyOnWriteArrayList: copy every time you modify the container, where iteration requirements are greater than modification requirements.

The producer-consumer pattern USES bounded blocking queues to decouple producer and consumer code.

The Executor task execution framework implements the producer-consumer pattern.

SynchronousQueue: put waits for the consumer to be available, take waits for the producer to be available, suitable for situations where the consumer is plentiful.

Deque (deque) is associated with the steal work mode (work stealing), different from producers to consumers all consumers in the mode of sharing a work queue, work stealing mode 1 each consumer has its own deque, if a consumer completed all his work, you can steal other consumer queue of the task.

The work stealing pattern applies when running to a unit of a task, where more tasks, such as traversing files, may be identified.

When a method can throw InterruptedException, it is a blocking method. Throw or catch InterruptedException.

Synchronizer: synchronizer --semaphore, barrier, latch, encapsulates the state, determines the behavior of the thread in this state (through or through blocking), provides a means of manipulating the state, and effectively waits for the synchronizer to enter the desired state.

latch blocking: delays thread progress until the thread reaches an endpoint state, like a primary switch. Can be used to ensure that a particular activity does not occur until other activities have completed.

Such as:

Ensure that a calculation is not performed until the required resources are initialized. Ensure that a service is not started until the other dependent service has started. Wait until all parts of the activity are ready to proceed.

FutureTask can be used as a latching, abstract computation of portable results, implemented by callable. Future.get depends on the execution state of the task, and the result is returned when the task is completed.

The Executor framework USES FutureTask for asynchronous tasks.

semapher semaphores: used to control the number of activities that can simultaneously access a particular resource or perform a given operation at the same time, resource pools, container boundaries.

barrier level: similar to an interlock, except that all threads must arrive at the level at the same time to continue processing. The interlock waits for the time, while the level waits for other threads, which are reused. Through the level, await returns a unique arrival index number for each thread, which can be used to elect a leader to take on special tasks in the next iteration.

Exchanger1 in the form of levels.


Related articles: