Implementation of the Java API (next)

Source: Internet
Author: User
Tags wrapper

Wrapper implementation

Wrapper implementations are implementations that delegate their actual work to a particular set of objects, which adds additional functionality to the functionality provided by the set of objects. This is a decorator (painter) style for design patterns enthusiasts. It's a little exotic, but it's really simple.

These implementations are anonymous: instead of providing a common class, the JDK provides a static factory method. All of these can be found in the collections API, which contains only static methods.

Sync Wrapper (Synchronization Wrappers)

The synchronization wrapper adds automatic synchronization (thread-safe) to an arbitrary set of objects. Each of the 6 core object set interfaces corresponds to a static method:

public static Collection synchronizedCollection(Collection c);
public static Set synchronizedSet(Set s);
public static List synchronizedList(List list);
public static Map synchronizedMap(Map m);
public static SortedSet synchronizedSortedSet(SortedSet s);
public static SortedMap synchronizedSortedMap(SortedMap m);

Each of these methods returns a Collection of a particular set of objects as a fallback synchronization (thread-safe). To ensure serial access, it is essential that all access to the set of fallback objects be done through the returned set of objects. An easy way to ensure this is to not keep a reference to the set of reserved objects, and creating such a set of synchronized objects is a small trick:

List List = Collections.synchronizedlist (new ArrayList ());

A set of objects created in this way, each bit is thread-safe, just like Vectorm's "normal" set of synchronized objects. In cases where concurrent access is required, it is necessary for the user to manually synchronize the returned set of objects when iterating over the returned object set. This is because iterations are done through multiple invocations of the set of objects, which must be written as a single minimum unit operation (atomic operation). The iterative idioms on the set of objects that a wrapper synchronizes are as follows:

Collection c = Collections.synchronizedCollection(myCollection);
synchronized(c) {
Iterator i = c.iterator(); // Must be in the synchronized block!
while (i.hasNext())
foo(i.next());
}

The iterative idiom on the collection view of a synchronized map is similar to the one above, but there is a knack that when iterating over the collection view of the synchronized map, the user must synchronize the synchronization map manually, rather than synchronizing the collection view itself :

Map m = Collections.synchronizedMap(new HashMap());
...
Set s = m.keySet(); // Needn"t be in synchronized block
...
synchronized(m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}

A small flaw in the wrapper implementation is that you cannot perform a wrapper implementation without the interface operation. So, for example, in the above list example, you can't invoke the ensurecapacity operation on the ArrayList of the wrapper.

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.