JUC练习8——队列

一,java中队列实现的位置(在多线程并发处理,和线程池计算中使用到并发技术)

 

JUC练习8——队列

 

 二,阻塞队列的基本操作:添加,移除元素

JUC练习8——队列

 

代码示例:

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class JUCTest {
    /**
     * add 添加后超过容量抛异常,否则返回true
     * remove 队列中没有元素抛异常,否则返回元素
     */
    @Test
    public void test1()
    {
        ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>(2);
        System.out.println(blockingQueue1.add("a"));
        System.out.println(blockingQueue1.add("b"));
//        blockingQueue1.add("C");
        System.out.println(blockingQueue1.remove());
        System.out.println(blockingQueue1.remove());
        System.out.println(blockingQueue1.remove());
    }

    /**
     * offer 添加后超过容量返回false
     * remove 队列中没有元素返回null
     */
    @Test
    public void test2()
    {
        ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>(2);
        System.out.println(blockingQueue1.offer("a"));
        System.out.println(blockingQueue1.offer("b"));
        System.out.println(blockingQueue1.offer("C"));
        System.out.println(blockingQueue1.poll());
        System.out.println(blockingQueue1.poll());
        System.out.println(blockingQueue1.poll());
    }

    /**
     * put 添加后超过容量,线程会一直等待队列有位置时,将元素插入队列
     * take 队列中没有元素,一直等待元素的取出
     */
    @Test
    public void test3() throws InterruptedException {
        ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>(2);
        blockingQueue1.put("A");
        blockingQueue1.put("B");
//        blockingQueue1.put("C");
        System.out.println(blockingQueue1.take());
        System.out.println(blockingQueue1.take());
        System.out.println(blockingQueue1.take());
    }

    /**
     * 超时等到,如果两秒中元素的添加和移除还没有满足添加就跳过
     * put 添加后超过容量,线程会一直等待队列有位置时,将元素插入队列
     * take 队列中没有元素,一直等待元素的取出
     */
    @Test
    public void test4() throws InterruptedException {
        ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>(2);
        blockingQueue1.offer("A");
        blockingQueue1.offer("B");
        blockingQueue1.offer("C",2, TimeUnit.SECONDS);
        System.out.println(blockingQueue1.poll());
        System.out.println(blockingQueue1.poll());
        System.out.println(blockingQueue1.poll(2,TimeUnit.SECONDS));
    }
}

  

 三,同步队列

/**
     * 同步队列,不存储元素
     * 只有先存入一个元素后才能取元素,如果元素没有被取出,就不能存入元素,线程等待
     * 使用for循环,有点问题
     */
    public static void main(String[] args) {
        test5();
    }
    //@Test
    public static void test5() {
        BlockingQueue<Integer> synchronousQueue = new SynchronousQueue<>();
        new Thread(()->{

            try {
                synchronousQueue.put(1);
                System.out.println(Thread.currentThread().getName()+" put "+1);
                synchronousQueue.put(2);
                System.out.println(Thread.currentThread().getName()+" put "+2);
                synchronousQueue.put(3);
                System.out.println(Thread.currentThread().getName()+" put "+3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" get "+synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" get "+synchronousQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" get "+synchronousQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();
    }

执行结果:

A put 1
B get 1
A put 2
B get 2
A put 3
B get 3

上一篇:【Beautiful JUC Part.1】建立起Java并发体系的大厦


下一篇:JDK源码那些事儿之SynchronousQueue上篇