Java Concurrency Programming (11) instance closure

Source: Internet
Author: User
Tags closure

topic in this section: What do you do if a class is thread insecure but is used safely in multithreaded programs?

There are basically two ways of thinking:

The first is to make sure that the object is accessible by a single thread, that is, the object is enclosed in threads;

The second type is to protect access to the object by locking it.

Instance closure

Encapsulation simplifies the implementation of thread-safe classes and provides an instance closure mechanism (Instance confinement).

When an object is encapsulated in another object, all code paths that can access the encapsulated object are known, and are easier to parse than the unsafe classes scattered throughout the program. By combining a closed mechanism with an appropriate locking policy, you can ensure thread-safe ways to use non-thread-safe objects.

The enclosing object must not exceed the existing scope, and the object can be enclosed in an instance of the class (as a private member);

Either closed within a scope (for example, as a local variable), or closed online range (one method is passed to another method instead of sharing the object in multiple threads).

Note that the object itself will not escape at the time of publication.

Private one object;

Control the access path of the object;

Plus the appropriate locking mechanism.

Instance closure is one of the simplest ways to build a thread-safe class, and it also gives you more flexibility in the choice of lock strategies. Instance closures allow different state variables to be protected by different locks, while other forms of locking require the same lock throughout.

Collections.synchronizedlisty and its similar methods make it possible for non-thread-safe classes to be used in multithreaded environments. These factory methods encapsulate the container class in a synchronous wrapper object through the adorner pattern, and the wrapper object can implement each method in the interface as a synchronous method and forward the call request to the underlying container object. It is thread-safe as long as the wrapper object has a unique reference to the underlying container object (that is, enclosing the underlying container object in the wrapper).

Java Monitor mode

The Java Monitor mode object encapsulates all the mutable states of an object and is protected by the object's own built-in lock.

There are many advantages to using a private lock object instead of an object's built-in lock (or any other lock that can be accessed in a shared manner). A private lock object can encapsulate the lock so that the client cannot get the lock.

Java Concurrency Programming (11) instance closure

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.