Java concurrency Programming (ii)

Source: Internet
Author: User

designing a thread-safe class

There are three basic elements to note in the process of designing a thread-safe class:

1. Find all the variables that make up the object

2. Find the invariant condition of constraint state variable

3, establish the object state of the concurrent access management policy

A synchronization policy defines how to collaborate on access to its state without violating the object's invariant or post-condition conditions. The synchronization policy specifies how immutability, thread closures, and locking mechanisms can be combined to maintain thread security and which variables are protected by which locks. Ensure that this class can be analyzed and maintained, you need to write the synchronization policy as a formal document.

If you do not understand the object's invariant conditions, you cannot ensure thread safety. To satisfy the various constraints on the valid values of state variables or state transitions, it is necessary to use atomic and encapsulation.

instance Closure

Encapsulation simplifies the implementation of thread-safe classes, encapsulates data within objects, and restricts access to the object's methods, making it easier to ensure that threads always hold the correct locks when accessing data.

Encapsulation is easier to construct a thread-safe class, because when you encapsulate the state of a class, you do not have to examine the entire program when parsing the thread security of the class.

Thread-Safe delegationif a class consists of multiple independent, thread-safe state variables and does not contain invalid state transitions in all operations, then thread security can be delegated to the underlying state variable. If a state variable is thread-safe and there is no invariant condition to constrain its value, there is no state transition that is not allowed in the operation of the variable, so it is safe to publish the variable for a long time. Synchronizing Container classes

In the early JDK, the synchronization containers were only vectors and Hashtable, Later, Colletions.synchronizedxx was introduced to create the synchronization containers, which are thread-safe by encapsulating their state and synchronizing each public method so that only one thread can access the state of the container at a time.

Although the synchronization container class is thread-safe, in some cases additional client locks are required to protect the composite operation.

Concurrent Containers

JAVA5.0 provides a variety of concurrent containers to improve the performance of the synchronization container. Synchronization containers serialize all access to the container state to achieve their thread safety, and the cost of this approach is to severely reduce concurrency. Throughput is severely degraded when multiple threads compete for container locks.

Replacing synchronous containers with concurrent containers can greatly improve scalability and reduce risk.

The common classes are concurrenthashmap,copyonwritearraylist,blockingqueue,

Concurrentlinkedqueue,concurrentskiplistmap and so on.

Synchronization Tool Class

Latching is a synchronization tool class that can delay the progress of a thread until it reaches the terminating state. The Countdownlactch is a flexible latching implementation.

Futuretask can also be used as latching.

The count semaphore semaphore is used to control the number of operations that concurrently access a particular resource, or to perform an operation at the same time.

Fences can block a set of threads until an event occurs. The key difference between fencing and latching is that all threads must reach the fence position at the same time to continue execution. Latching is used to wait for events while fences are used to wait for other threads. Cyclicbarier is a fence implementation.






Welcome to scan QR Code, follow the public number



Java concurrency Programming (ii)

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.