Discussion on Java thread concurrency knowledge point _java

Source: Internet
Author: User
Tags semaphore

Publish: An object is a reference that enables it to be referenced by code outside the current scope:

Common form: To store references to a public static domain, to return a reference in a private method, to publish an internal class instance, and to include a reference.

Escape: Publish the object when it is not ready.

Do not let this reference escape in the constructor. example, a thread is started in the constructor, and the thread contains a reference to the object.

Sync container: Traverse access to all States of the container, Vector, hashtable,cllections.synchronizedmap| List

Concurrent containers: Concurrenthashmap,copyonwritearraylist,concurrentlinkedqueue, Blockingqueue

The advantages of the list random access feature.

Blocking adds a blocking get set operation

Concurrenthashmap: Separation of locks, high throughput for concurrent access, and virtually no loss of access performance for individual threads. Returns a weakly consistent iterator.

The weak consistency of the iterator, which detects changes to the container after the iterator is generated.

Concurrent container size (), IsEmpty () weakening, returns approximate results.

Copyonwritearraylist: Copy every time the container is modified, the applicable iterative requirements are greater than the modification requirements.

Producer consumer model, using bounded blocking queues to decouple producer and consumer code.

The executor task execution framework realizes the producer consumer model.

The synchronousqueue:put waits for the consumer to be available, take waits for the producer to be usable, applies to the consumer sufficient situation.

The dual-end queue (deque) is associated with the stealing mode of work (work stealing), which distinguishes all consumers in the producer consumer model from sharing a Task Force column, the work-stealing model each consumer has its own two-terminal queue, if a consumer completes all of its work, You can steal the task of other consumer queue teams.

The work-stealing pattern applies when you run to a single unit of a task, and you may identify more tasks, such as traversing a file.

When a method can throw a interruptedexception, it means that it is a blocking method. Throws or captures Interruptedexception.

Synchronizer: The Synchronizer---semaphore, barrier, latch, encapsulation State, determines the behavior (through or blocking) of the thread in this state, provides a way to manipulate the state, and efficiently waits for the Synchronizer to enter the desired state.

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

For example:

    • Make sure that a calculation is not executed until the required resources are initialized.
    • Ensure that a service is not started until other dependent services have started.
    • Wait until all parts of the activity are ready to continue processing.

Futuretask can be used as a latching, abstract, portable result of the calculation, through callable implementation. Future.get depends on the execution state of the task, the task is completed, the result is returned, or it waits.

The executor framework uses Futuretask to complete asynchronous tasks.

Semapher Semaphore: The amount, resource pool, container boundary, that is used to control the amount of activity that can access a particular resource at the same time or at the same time perform a given operation.

Barrier level: Similar to the blocking, all the threads must be at the same time to reach the checkpoint in order to continue processing, blocking waiting is the time, the checkpoint waiting for other threads, re-use. Through the checkpoint, await will return the unique arrival index number for each thread, which can be used to elect a leader and undertake some special tasks in the next iteration.

Exchanger the form of a checkpoint.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

Related Article

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.