On the knowledge points of java thread concurrency

Publish: an object that enables it to be referenced by code outside the current scope:

Common forms: store the reference of the object in the public static domain; Return reference in non private method; Publish internal class instances, including references.

Escape: publish an object when it is not ready.

Don't let this reference escape in the constructor. For example, start a thread in the constructor, and the thread will contain a reference to the object.

Synchronize container: walkthrough access to all States of the container, such as vector, hashtable, and collections synchronizedMap|List

Concurrent containers: concurrenthashmap, copyonwritearraylist, concurrentlinkedqueue, BlockingQueue

The advantages of the random access feature of list.

Blocking adds a blocking get set operation

Concurrent HashMap: separation of locks brings high throughput to concurrent access, and almost no loss of access performance of a single thread. Returns an iterator with weak consistency.

The weak consistency of the iterator will detect the changes of the container after the iterator is generated.

The concurrent container size(), isempty(), returns approximate results.

Copyonwritearraylist: copy every time the container is modified. The applicable iteration requirements are greater than the modification requirements.

Producer consumer model uses bounded blocking queue to decouple producer and consumer code.

The executor task execution framework implements the producer consumer model.

Synchronousqueue: put waits for the consumer to be available and take waits for the producer to be available, which is applicable to the scenario where the consumer has enough.

Deque is associated with the work stealing mode, which is different from the producer consumer mode in which all consumers share a work queue. In the work stealing mode, each consumer has its own double ended queue. If a consumer completes all his work, he can steal the tasks at the end of other consumer queues.

The work stealing mode is applicable to more tasks that may be recognized when running to a unit of a task, such as traversing files.

When a method can throw interruptedexception, it indicates that 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 blocking), provides methods to manipulate the state, and efficiently waits for the synchronizer to enter the desired state.

Latch lock: delay the thread progress until the thread reaches an end state, like a one-time switch. Can be used to ensure that specific activities do not occur until other activities are completed.

For example:

Futuretask can be used as the calculation of locking and abstract portable results, which can be realized through callable. Future. Get depends on the execution status of the task. When the task is completed, the result will be returned. Otherwise, it will wait all the time,

The executor framework uses futuretask to complete asynchronous tasks.

Semaphore: used to control the number of activities that can access a specific resource at the same time or perform a given operation at the same time, resource pool and container boundary.

Barrier level: similar to blocking. The difference is that all threads must reach the level at the same time before processing can continue. Blocking waits for time, and the level waits for other threads. It is reused. Through the level, await will return a unique arrival index number for each thread, which can be used to elect a leader to undertake some special tasks in the next iteration.

Exchange is a form of checkpoint.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>