關(guān)于ConcurrentHashMap
在并發(fā)編程中,經(jīng)常會(huì)用到ConcurrentHashMap倒谷,因?yàn)镠ashMap是非線程安全的茬末,而ConcurrentHashMap是線程安全的婿禽。項(xiàng)目中通常會(huì)對(duì)ConcurrentHashMap進(jìn)行封裝掺涛,此時(shí)就會(huì)涉及到對(duì)ConcurrentHashMap擴(kuò)容的操作。
代碼實(shí)現(xiàn):
<pre>
import java.util.concurrent.ConcurrentHashMap;
/**
-
Created by lenvovo on 2016/9/23.
*/
public class TestCopy {public static ConcurrentHashMap<String,String> names = new ConcurrentHashMap<>(6666);
static {
for(int i=0;i<18;i++){
names.put("jack"+i,"jacklee"+i);
}
}
public static void main(String []args){
ConcurrentHashMap<String,String> copy_names = new ConcurrentHashMap<>(88888);
for(ConcurrentHashMap.Entry<String,String> name:names.entrySet()){
System.out.println(name.getKey()+'\t'+name.getValue());
copy_names.put(name.getKey(),name.getValue());} System.out.println("nmaes size -----:"+names.size());//返回的是含有數(shù)據(jù)的大小-->18,而不是容量大小 System.out.println("nmaes size -----:"+copy_names.size()); for(ConcurrentHashMap.Entry<String,String> copy_name:copy_names.entrySet()){ System.out.println(copy_name.getKey()+'\t'+copy_name.getValue()); }
}
}
</pre>
經(jīng)測(cè)試匿情,以上代碼能實(shí)現(xiàn)ConcurrentHashMap數(shù)據(jù)的復(fù)制兰迫,即能實(shí)現(xiàn)ConcurrentHashMap的擴(kuò)容。
參考: