-
ThreadLocal
可以看做是一个Map ,key 相当于Thread.getCurrentTread
value 存放线程需要保存的变量。ThreadLocal.set(value) ==> map.put(Thread.getCurrentTread,value);
TreadLocal.get(); ==> map.get(Thread.getCurrentThread());
在使用TreadLocal 时,需要注意资源回收,每个线程结束前,将当前线程保存的线程变量一定要删除。TreadLocal.remove();
2, 常见的并发容器
1> ConcurrentHashMap
2>ConcurrentSkipListMap
3>CopyOnWriteList
4>ConcurrentLinkedQueue 队列,链表实现
常用方法:queue.offer(“test”) //添加元素
queue.size() //查看队列长度
queue.peek() //查看对列中的首元素
queue.poll() //获取队列中的首元素,队列长度会减少
5>BlockingQueue 的实现:
LinkedBlockingQueue:
有put和take 方法,队列满或者为空时会自动阻塞。
ArrayBlockingQueue:
add 方法:在容量不足的情况下直接抛出异常
put 方法:在容量不足的时候会阻塞,是可以打断的
offer方法:单参数无阻塞,容