71154

java并发队列

<h1 id="阻塞队列">阻塞队列

常见的阻塞队列有ArrayBlockingQueue,LinkedBlockingDeque,LinkedBlockingQueue,这些队列有界且可以阻塞线程

<h2 id="arrayblockingqueue">ArrayBlockingQueue</h2>

ArrayBlockingQueue是有界的阻塞队列,LinkedBlockingDeque通过数组、非空和非满Condition、RantrantLock实现了线程安全。空的时候take阻塞,满的时候put阻塞

<h2 id="java并发初探linkedblockingqueue">java并发初探LinkedBlockingQueue</h2>

LinkedBlockingQueue是有界的阻塞队列,通过head,last节点,putLock插入锁,takeLock取锁、非空和非满condition、单向链表实现了线程安全的阻塞队列,空的时候take阻塞,满的时候put阻塞

<h2 id="java并发初探linkedblockingqueue-1">java并发初探LinkedBlockingQueue</h2>

LinkedBlockingQueue是有界的阻塞队列,通过head,last节点,putLock插入锁,takeLock取锁、非空和非满condition、单向链表实现了线程安全的阻塞队列,空的时候take阻塞,满的时候put阻塞

<h1 id="非阻塞队列">非阻塞队列

<h2 id="java并发初探concurrentlinkedqueue">java并发初探ConcurrentLinkedQueue</h2>

ConcurrentLinkedQueue是线程安全的无界队列,通过volatile和CAS操作保证了线程安全。

<h2 id="例子">例子</h2>

阻塞队列当满的时候put元素回阻塞,实现队列接口的LinkedList在多线程的时候会跑出ConcurrentModifExeception异常

package com.java.javabase.thread.collection; import lombok.extern.slf4j.Slf4j; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingQueue; /** * @author */ @Slf4j public class BlockingQueueTest { /* public static Queue<String> queue = new ArrayBlockingQueue<String>(20); public static ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(20); public static LinkedBlockingDeque<String> queue = new LinkedBlockingDeque<String>(20); public static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(20); public static Queue<String> queue =new LinkedList<String>(); public static ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>(); */ public static ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(20); public static int size = 15; public static void main(String[] args) { Thread t1 = new InnerThread("t1"); Thread t2 = new InnerThread("t2"); t1.start(); t2.start(); try { Thread.sleep(2000); printQueue(queue); } catch (InterruptedException e) { e.printStackTrace(); } } static class InnerThread extends Thread { String name; public InnerThread(String name) { super(name); this.name=name; } @Override public void run() { for (int i = 0; i < size; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } String tmp=i+" "+name; //queue.add(tmp); try { queue.put(tmp); //queue.add(tmp); } catch (InterruptedException e) { log.error("error",e); } printQueueNone(queue); } } } public static void printQueue(Queue queue) { Iterator iterator = queue.iterator(); while (iterator.hasNext()) { log.info("thread {} print {}", Thread.currentThread().getName(), iterator.next()); } } public static void printQueueNone(Queue queue) { Iterator iterator = queue.iterator(); while (iterator.hasNext()) { iterator.next(); } } } <h2 id="运行结果">运行结果</h2> 2019-08-28 19:42:32,684 [main] INFO BlockingQueueTest - thread main print 0 t1 2019-08-28 19:42:32,686 [main] INFO BlockingQueueTest - thread main print 0 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 1 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 1 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 2 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 2 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 3 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 3 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 4 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 4 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 5 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 5 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 6 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 6 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 7 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 7 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 8 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 8 t1 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 9 t2 2019-08-28 19:42:32,687 [main] INFO BlockingQueueTest - thread main print 9 t1

来源:博客园

作者:云碧霄

链接:https://www.cnblogs.com/JuncaiF/p/11426096.html

Recommend