Thread synchronization mechanism (ii)--Thread Synchronization helper class

Source: Internet
Author: User
Tags semaphore

Our thread synchronization mechanism (i)--synchronized and lock Brief Introduction to the concept of synchronization and critical areas, and discussed the synchronization of multiple concurrent tasks sharing a resource. A block of code that accesses a shared resource is called a critical section.

Our thread synchronization mechanism (i)--synchronized and lock Brief Introduction to learn the content:

    • Synchronized keywords

    • Lock interfaces and their implementation classes, such as Reentrantlock,reentrantreadwritelock.readlock and Reentrantreadwritelock.writelock

In this chapter we will learn how to use a more advanced synchronization mechanism to achieve synchronization between multiple threads.

    • Semaphore (Semaphore): A counter that is used to protect access to one or more shared resources. It is an underlying tool for concurrent programming.

    • Countdownlatch: is a synchronization helper class provided by the Java language that allows threads to wait until a set of operations that are being performed in another thread is completed

    • Cyclicbarrier:java language provides a synchronization helper class that allows multiple threads to wait at a collection point

    • Phaser: It runs concurrent tasks in multiple stages, and all threads in the current phase must perform before the next phase begins.

    • Exchanger: It provides a data exchange point between two threads.

In the application, semaphore can be used at any time to protect the critical section because it is a basic synchronization mechanism.

1. Concurrent access control for resources.

The Java language provides the semaphore (Semaphore) mechanism. A semaphore is a counter that is used to protect access to one or more shared resources. If a thread wants to access a shared resource, it must obtain a semaphore. If the internal counter of the semaphore is greater than 0, the semaphore is reduced by 1, and then the shared resource is allowed access. A counter greater than 0 means there is a resource available, so the thread is allowed to use one of the resources. Otherwise, if the counter of the signal equals 0, the semaphore will put the thread into hibernation until the counter is greater than 0. When the counter equals 0, it means that all shared resources have been used by other threads, so the thread that needs access to the shared resource must wait.

When a thread finishes using a shared resource, the semaphore must be freed so that other threads can access the shared resource. The release operation increases the semaphore internal counter by 1.

Using semaphores to achieve a critical section must follow 3 steps to protect access to resources.

(1). The semaphore must be obtained through the acquire () method

(2). Use shared resources to perform necessary actions

(3). The semaphore must be released through the release () method

2. Wait for multiple concurrent events to complete

The Java Concurrency API provides the Countdownlatch class. It allows threads to wait until a set of operations is completed in another thread. This class is initialized with an integer that is the number of operations the thread waits to complete. The await () method needs to be called when a thread waits for certain operations to finish first. When a thread operation is complete, it will call the countdown () method to reduce the internal counter by 1. When the counter becomes 0, the Countdownlatch class wakes all calls to await () and enters the dormant thread

There are three basic elements of the Countdownlatch class:

    • An initial value that defines the operational data that must wait for the antecedent to be completed;

    • Await () method, which needs to wait for the other event to complete before the thread calls

    • The countdown () method, in which each awaited event is called after completion of the operation, minus 1 of the internal counter. When the internal counter arrives 0 o'clock, the Countdownlatch object wakes up all the threads waiting on the await () method

3. Operation of concurrent phase tasks

The Java Concurrency API also provides phaser, which allows concurrent multi-stage tasks to be performed. This mechanism is useful when we have concurrent tasks and need to be broken down into several steps to execute. The Phaser class mechanism synchronizes threads at the end of each step, allowing the next step to be performed when all the threads have performed the steps. The Phaser class provides the Onadvance () method, which is executed automatically when the Phaser phase changes.

4, the data exchange between concurrent tasks exchanger. It allows for data exchange between concurrent tasks, specifically, the exchanger class allows two threads to define synchronization points. When two threads reach the synchronization point, they exchange data structures.

This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1787744

Thread synchronization mechanism (ii)--Thread synchronization helper class

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.