concurrent container

copyandwrite container
CopyOnWriteArrayList
CopyOnWriteArraySet
CopyOnWriteArrayList and CopyOnWriteArraySet replace List and Set respectively,
It is mainly to replace the synchronized List and the synchronized Set when the traversal operation is the main method. This is the idea mentioned above: the iteration process must ensure that there is no error. In addition to locking, another method is to "clone" the container object.
: Suitable for reading more and writing less. Read-Write Separated Containers
 
8 ConcurrentHashMap concurrent container
The java.util.concurrent package was introduced. with Vector and Hashtable,
Compared with the collections.synchronizedXxx() synchronization container, the concurrent container introduced in util.concurrent mainly solves two problems: 
1) Design according to specific scenarios, try to avoid synchronization and provide concurrency. 
2) Some concurrent and safe composite operations are defined, and iterative operations in a concurrent environment are guaranteed not to go wrong. ConcurrentLinkedQueue is a first-in, first-out queue. It is a non-blocking queue.
ConcurrentSkipListMap can replace SoredMap (such as TreeMap wrapped with Collections.synchronzedMap) in efficient concurrency.
ConcurrentSkipListSet can replace SoredSet (such as TreeMap wrapped with Collections.synchronzedSet) in efficient concurrency.
ConcurrentHashMap replaces hashtable
--------------------------------------------------------- 
CopyOnWriteMap is not provided in JDK. We can refer to CopyOnWriteArrayList to implement one. The basic code is as follows:
import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class CopyOnWriteMap<K, V> implements Map<K, V>, Cloneable {
    private volatile Map<K, V> internalMap;

    public CopyOnWriteMap() {
        internalMap = new HashMap<K, V>();
    }

    public V put(K key, V value) {

        synchronized (this) {
            Map<K, V> newMap = new HashMap<K, V>(internalMap);
            V val = newMap.put(key, value);
            internalMap = newMap;
            return val;
        }
    }

    public V get(Object key) {
        return internalMap.get(key);
    }

    public void putAll(Map<? extends K, ? extends V> newData) {
        synchronized (this) {
            Map<K, V> newMap = new HashMap<K, V>(internalMap);
            newMap.putAll (newData);
            internalMap = newMap;
        }
    }
}
 
 

Application scenarios of CopyOnWrite

CopyOnWrite并发容器用于读多写少的并发场景。比如白名单,黑名单,商品类目的访问和更新场景,假如我们有一个搜索网站,用户在这个网站的搜索框中,输入关键字搜索内容,但是某些关键字不允许被搜索。这些不能被搜索的关键字会被放在一个黑名单当中,黑名单每天晚上更新一次。当用户搜索时,会检查当前关键字在不在黑名单当中,如果在,则提示不能搜索。实现代码如下:

package com.ifeve.book;

import java.util.Map;

import com.ifeve.book.forkjoin.CopyOnWriteMap;

/**
 * 黑名单服务
 *
 * @author fangtengfei
 *
 */
public class BlackListServiceImpl {

    private static CopyOnWriteMap<String, Boolean> blackListMap = new CopyOnWriteMap<String, Boolean>(
            1000);

    public static boolean isBlackList(String id) {
        return blackListMap.get(id) == null ? false : true;
    }

    public static void addBlackList(String id) {
        blackListMap.put(id, Boolean.TRUE);
    }

    /**
     * 批量添加黑名单
     *
     * @param ids
     */
    public static void addBlackList(Map<String,Boolean> ids) {
        blackListMap.putAll(ids);
    }

}

 代码很简单,但是使用CopyOnWriteMap需要注意两件事情:

1. 减少扩容开销。根据实际需要,初始化CopyOnWriteMap的大小,避免写时CopyOnWriteMap扩容的开销。

2. 使用批量添加。因为每次添加,容器每次都会进行复制,所以减少添加次数,可以减少容器的复制次数。如使用上面代码里的addBlackList方法。

CopyOnWrite的缺点

CopyOnWrite容器有很多优点,但是同时也存在两个问题,即内存占用问题和数据一致性问题。所以在开发的时候需要注意一下。

内存占用问题。因为CopyOnWrite的写时复制机制,所以在进行写操作的时候,内存里会同时驻扎两个对象的内存,旧的对象和新写入的对象(注意:在复制的时候只是复制容器里的引用,只是在写的时候会创建新对象添加到新容器里,而旧容器的对象还在使用,所以有两份对象内存)。如果这些对象占用的内存比较大,比如说200M左右,那么再写入100M数据进去,内存就会占用300M,那么这个时候很有可能造成频繁的Yong GC和Full GC。之前我们系统中使用了一个服务由于每晚使用CopyOnWrite机制更新大对象,造成了每晚15秒的Full GC,应用响应时间也随之变长。

针对内存占用问题,可以通过压缩容器中的元素的方法来减少大对象的内存消耗,比如,如果元素全是10进制的数字,可以考虑把它压缩成36进制或64进制。或者不使用CopyOnWrite容器,而使用其他的并发容器,如ConcurrentHashMap

数据一致性问题CopyOnWrite容器只能保证数据的最终一致性,不能保证数据的实时一致性。所以如果你希望写入的的数据,马上能读到,请不要使用CopyOnWrite容器。

关于C++的STL中,曾经也有过Copy-On-Write的玩法,参见陈皓的《C++ STL String类中的Copy-On-Write》,后来,因为有很多线程安全上的事,就被去掉了。

原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: 聊聊并发-Java中的Copy-On-Write容器

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326193333&siteId=291194637