HashMap

下标计算方法:hashCode & (length-1) ,&按位与操作,二进制相同位都是1,该位才为1

JDK1.7与JDK1.8中HashMap区别: JDK1.8在put元素时,若计算得到的下标下的链表长度达到8时(算上当前要加到链表尾部的元素),链表会转换成红黑树提高查找效率

通过调整hashmap初始大小避免rehash的实例:要转移1000个数据到hashmap,那么假定初始大小1024,在转移到1024*0.75=768个时会触发rehash,将1024扩容到2048,所以如果在设置初始大小时指定2048就可以避免一次rehash

https://www.cnblogs.com/williamjie/p/9358291.html

hashMap是线程不安全的,hashtable利用synchronized加在put和get方法上实现线程安全

ConcurrentHashMap

线程安全的HashMap

JDK1.7 ConcurrentHashMap有多个HashTable组成,有几个HashTable并发级别concurrencyLevel就是几,默认16,这就叫分段锁,

ConcurrentSkipListMap/TreeMap

TreeMap与HashMap不同之处在于,TreeMap按照key的字典顺序来排序(升序),也可通过Comparator实现排序逻辑

TreeMap线程不安全,使用ConcurrentSkipListMap保证线程安全

ArrayList

非线程安全

CopyOnWriteArrayList

线程安全,通过ReentrantLock实现,add()方法加锁

但会对数组进行拷贝,在某段时间数据量翻倍造成内存溢出,不适用数据量过大的情况

读取新写入的数据会有延迟,因为数组的引用需要从旧数组转到新数组

     /**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len + 1);
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}

HashSet

HashMap如果遇到相同的Key会对Value值进行覆盖,保证HashMap的Key是不重复的。

HashSet<T> 即利用HashMap这一特性保证Set集合中元素(KEY)不重复,其并不关心Value的值。

CopyOnWriteArraySet

写的时候会判断数组中是否已经存在,若存在就不再写入,其他特性与CopyOnWriteArrayList相同

ConcurrentSkipListSet

跳表实现原理同ConcurrentSkipListMap,KEY不重复且有序

队列QUEUE

ArrayBlockingQueue

线程安全,数组实现,ReentrantLock锁,只有一把锁,take() 和 put() 互斥,加的是同一把锁;

take()和put()方法是阻塞的,offer()和poll()方法是非阻塞的

构造函数制定队列长度capacity,利用参数count记录队列中元素数量,count=capacity时put操作阻塞(poll()方法直接返回null),count=0时take操作阻塞(offer()方法直接返回false),底层唤醒其实是利用condition的2个队列实现,参照1.3.1 Lock利用condition实现的阻塞队列;

put和take时利用takeIndex和putIndex记录下一个操作应该放到那个位置或从哪个位置取;

循环数组实现:当putIndex达到capacity时,takeIndex并不是+1,而是变成0,即下一个put操作会将元素放到队列头部;takeIndex原理相同

 public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length)
notFull.await();
enqueue(e);
} finally {
lock.unlock();
}
} public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
} public boolean offer(E e) {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (count == items.length)
return false;
else {
enqueue(e);
return true;
}
} finally {
lock.unlock();
}
} public E poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return (count == 0) ? null : dequeue();
} finally {
lock.unlock();
}
}

LinkedBlockingQueue

线程安全,链表实现,put和take各一把ReentrantLock锁,读和写操作加的是不同的锁,互不干扰,提高了读写性能

take()操作利用takeLock操作链表头部,put()利用putLock操作链表尾部,互不干扰,读和取不会互斥,并发度比ArrayBlockingQueue高一些

ConcurrentLinkedQueue

线程安全,并发度高,非阻塞队列,无锁,没有take() 和 put()这2个阻塞方法

利用CAS操作,采用自旋的方式将元素添加到链表尾部 或 将链表头部替换为null

SynchronousQueue

不常用

package com.study.list_set_queue.queue;

import java.util.concurrent.SynchronousQueue;

/*
1、take会阻塞,直到取到元素
2、put时会阻塞,直到被get
3、若没有take方法阻塞等待,offer的元素可能会丢失
4、poll取不到元素,就返回null,如果正好有put被阻塞,可以取到
5、peek 永远只能取到null,不能让take结束阻塞 */ public class Demo2_SyncQueueTest {
static SynchronousQueue<String> syncQueue = new SynchronousQueue<>(); //put时会阻塞,直到被get
public static void test01() throws InterruptedException {
new Thread(){
@Override
public void run() {
try {
Thread.sleep(3000L);
System.out.println(syncQueue.poll());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start(); System.out.println("begain to put...");
syncQueue.put("put_element");
System.out.println("put done...");
} //3、若没有take方法阻塞等待,offer的元素可能会丢失
public static void test02() throws InterruptedException {
syncQueue.offer("offered_element"); System.out.println(syncQueue.poll());
} //4、poll取不到元素,就返回null,如果正好有put被阻塞,可以取到
public static void test03() throws InterruptedException {
/* new Thread(){
@Override
public void run() {
try {
syncQueue.put("put_element");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();*/ Thread.sleep(200L);
Object obj = syncQueue.poll();
System.out.println(obj);
} //peek 永远只能取到null,不能让take结束阻塞
public static void test04() throws InterruptedException {
new Thread(){
@Override
public void run() {
try {
syncQueue.put("put_element");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start(); Thread.sleep(200L);
Object obj = syncQueue.peek();
System.out.println(obj);
} public static void main(String args[]) throws InterruptedException {
test02();
}
}

PriorityBlockingQueue

优先级队列,可以通过比较器对入队列的元素进行排序存储,进而改变出队列顺序

package com.study.list_set_queue.queue;

import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue; public class Demo4_PriorityBlockingQueue3 {
public static void main(String args[]) {
PriorityBlockingQueue<Student> queue = new PriorityBlockingQueue<>(5, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num1 = o1.age;
int num2 = o2.age; if (num1 > num2)
return 1;
else if (num1 == num2)
return 0;
else
return -1;
}
});
queue.put(new Student(10, "enmily"));
queue.put(new Student(20, "Tony"));
queue.put(new Student(5, "baby")); for (; queue.size() > 0;) {
try {
System.out.println(queue.take().name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} class Student {
public int age;
public String name; public Student(int age, String name) {
this.age = age;
this.name = name;
}
}

1.3.3 并发容器类MAP/LIST/SET/QUEUE的更多相关文章

  1. java并发容器(Map、List、BlockingQueue)

    转发: 大海巨浪 Java库本身就有多种线程安全的容器和同步工具,其中同步容器包括两部分:一个是Vector和Hashtable.另外还有JDK1.2中加入的同步包装类,这些类都是由Collectio ...

  2. java Concurrent并发容器类 小结

    Java1.5提供了多种并发容器类来改进同步容器的性能. 同步容器将所有对容器的访问都串行化,以实现他们的线程安全性.这种方法的代价是严重降低并发性,当多个线程竞争容器的锁时,吞吐量将严重减低.  一 ...

  3. java并发容器(Map、List、BlockingQueue)具体解释

    Java库本身就有多种线程安全的容器和同步工具,当中同步容器包含两部分:一个是Vector和Hashtable.另外还有JDK1.2中增加的同步包装类.这些类都是由Collections.synchr ...

  4. 《java并发编程实战》读书笔记4--基础构建模块,java中的同步容器类&并发容器类&同步工具类,消费者模式

    上一章说道委托是创建线程安全类的一个最有效策略,只需让现有的线程安全的类管理所有的状态即可.那么这章便说的是怎么利用java平台类库的并发基础构建模块呢? 5.1 同步容器类 包括Vector和Has ...

  5. go语言坑之并发访问map

    fatal error: concurrent map read and map write 并发访问map是不安全的,会出现未定义行为,导致程序退出.所以如果希望在多协程中并发访问map,必须提供某 ...

  6. 映射Map、队列Queue、优先级队列PriorityQueue

    映射Map 将对象映射到其他对象的能力是解决编程问题的有效方法.例如,考虑一个程序,它被用来检查 Java 的 Random 类的随机性.理想情况下, Random 会产生完美的数字分布,但为了测试这 ...

  7. Java并发编程(十四)并发容器类

    同步容器将所有对容器状态的访问都串行化,以实现线程安全性.这种方法的代价是严重降低并发性,当多个线程竞争容器的锁时,吞吐量将严重减低. 另一个方面,并发容器是针对多个线程并发访问设计的.在java 5 ...

  8. 专用于高并发的map类-----Map的并发处理(ConcurrentHashMap)

    oncurrentModificationException 在这种迭代方式中,当iterator被创建后集合再发生改变就不再是抛出ConcurrentModificationException, 取 ...

  9. JDK容器类Map源码解读

    java.util.Map接口是JDK1.2开始提供的一个基于键值对的散列表接口,其设计的初衷是为了替换JDK1.0中的java.util.Dictionary抽象类.Dictionary是JDK最初 ...

随机推荐

  1. tinymce+粘贴word图片例子

    tinymce是很优秀的一款富文本编辑器,可以去官网下载.https://www.tiny.cloud 这里分享的是它官网的一个收费插件powerpaste的旧版本源码,但也不影响功能使用. http ...

  2. pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path && FileNotFoundError: [WinError 2] 系统找不到指定的文件。

    C:\Users\k\Desktop\test>python test.py Traceback (most recent call last): File , in run_tesseract ...

  3. Egyptian Collegiate Programming Contest (ECPC 2015)

    题目链接:https://vjudge.net/contest/155219#overview. A题,用全排列来找出比当前这个数字字典序还大的排列有几个,然后前缀和dp即可.据说可以康拓展开来快速找 ...

  4. 清空echarts的option

    将相应的echarts的option治为空 $("#tt5sbmc").html("");

  5. 攻防世界Hello,CTF writeup

    解题过程 首先在ida中进行反汇编,查看main函数的代码: 代码的的36行处进行了一个字符串比较,如果v10的值等于v13的值会反馈一个success的输出.v13的值在第15行给出,因此需要知道v ...

  6. arcgis python 标注

    import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...

  7. <JavaScript> 普通函数与构造函数的区别

    JavaScript中存在构造函数与函数两个概念. 这可能会给初学者造成一定的困扰,难不成存在两种函数. 然而事实这两种函数的构成完全一样,无论从长相还是气质都是一模一样的. 区别点在于,所扮演觉得不 ...

  8. LC 984. String Without AAA or BBB

    Given two integers A and B, return any string S such that: S has length A + B and contains exactly A ...

  9. android studio gradle国内代理设置

    android studio在开始都各项目之前都会遇到 gradle 的同步,而在同步过程中很多依赖下载特别慢甚至出现无法现在的情况,有的时候等的时间特别长,甚至要一天,关键是等了大半天之后突然报错, ...

  10. CentOS源码安装 Tomcat/8.0.24

    依个人的习惯,喜欢将源码安装在/usr/local这个目录下面: 第一步:下载源码 wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.2 ...