队列和双端队列

PriorityQueue

此队列第一个元素永远是最小的,先进先出
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();

Deque

双端队列
Deque<String> dequeA = new LinkedList<>();
dequeA.add("element 1"); 
dequeA.addFirst("element 2");
dequeA.addLast("element 3");
dequeA.element(); // 获取队头元素
dequeA.getFirst();
dequeA.getLast();
dequeA.remove();
dequeA.removeFirst();
dequeA.removeLast();

BlockingQueue

线程阻塞队列
当你出队队空或者进队队满,会造成当前线程阻塞,除非此时其他的线程进队或者出队,才会继续运行
ArrayBlockingQueue
LinkedBlockingQueue
PriorityBlockingQueue

BlockingQueue<String> bQueue = new ArrayBlockingQueue<String>(2);

Queue

Queue<String> queue = new LinkedList<String>();
queue.offer( "first element" ); // 入队
queue.offer( "second element" );
while ( !queue.isEmpty() ) {
    System.out.println( queue.poll() ); // 出队
}

先进后出

Stack st = new Stack();
st.push(10);
st.pop();

集合并发

线程锁

List<String> threadSafeList = Collections.synchronizedList(new ArrayList<String>());
Set<String> threadSafeSet = Collections.synchronizedSet(new HashSet<String>());
Map<String, String> threadSafeMap = Collections.synchronizedMap(new HashMap<String, String>());

线程安全集合

List<String> threadSafeList = new CopyOnWriteArrayList<String>();
Set<String> threadSafeSet = new ConcurrentHashSet<String>();

Map<String, String> threadSafeMap = new ConcurrentHashMap<String, String>();
String previousValue = threadSafeMap.putIfAbsent("a", "1");

结语

本文章是java成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文

相关文章:

  • 2021-06-29
  • 2021-08-10
  • 2021-07-15
  • 2022-02-23
  • 2021-09-23
  • 2021-12-05
  • 2021-07-06
  • 2021-08-11
猜你喜欢
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案