测试 BlockingQueue

package blocking;

import org.junit.Test;
import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * BlockingQueue 是一个支持2个额外操作的队列,
 * 这2个额外操作指的是:队列为空时,取元素的线程会等待队列变为非空
 *                   队列满时,存储元素的线程会等待队列变为可用
 * BlockingQueue 适用场景:生产者和消费者模式
 *               生产者指的是向队列里添加元素的线程
 *               消费者指的是从队列里取元素的线程
 * BlockingQueue 7类具体子类型:
 * 1. ArrayBlockingQueue: 基于数组实现的有界阻塞队列
 * 2. LinkedBlockingQueue: 基于链表实现的有界阻塞队列
 * 3. PriorityBlockingQueue: 支持优先级的无界阻塞队列, 优先级指的是:元素自然排序
 * 4. DelayQueue: 支持延迟获取元素的无界阻塞队列
 * 5. SynchronousQueue:不存储元素的阻塞队列,存和取交替完成的
 * 6. LinkedTransferQueue: 基于链表实现的无界阻塞队列
 * 7. LinkedBlockingDeque: 基于链表实现的双向阻塞队列
 */
public class TestBlockingQueue {

    @Test
    public void testArrayBlockingQueue(){

        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
//        blockingQueue.offer("test1");
//        blockingQueue.offer("test2");
//        blockingQueue.offer("test3");
        for(int i=0;i<4;i++){
            System.out.print(blockingQueue.offer("test " + i + ";  "));
        }
        // truetruetruefalse
        for(int i=blockingQueue.size();i>0;i--){
            System.out.print(blockingQueue.poll());
        }
        // FIFO 原则
        // truetruetruefalse test 0;  test 1;  test 2;
    }

    @Test
    public void testLinkedBlockingQueue(){
        BlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(3);
        for(int i=0;i<4;i++){
            System.out.print(linkedBlockingQueue.offer("test" + i));
        }

        for(int i=linkedBlockingQueue.size();i>0;i--){
            System.out.print(linkedBlockingQueue.poll());
        }
        // truetruetruefalse test0test1test2
    }

    @Test
    public void testPriorityBlockingQueue(){
        PriorityBlockingQueue priorityBlockingQueue = new PriorityBlockingQueue(3);

        System.out.print(priorityBlockingQueue.offer("test1" ));
        System.out.print(priorityBlockingQueue.offer("test3" ));
        System.out.print(priorityBlockingQueue.offer("test2" ));
        System.out.print(priorityBlockingQueue.offer("test4" ));

        for(int i=priorityBlockingQueue.size();i>0;i--){
            System.out.print(priorityBlockingQueue.poll());
        }
        // truetruetruetrue test1test2test3test4
    }

    @Test
    public void testDelayQueue() throws InterruptedException {
        ArrayList list = new ArrayList<>();
//        list.add("test1");
//        list.add("test2");
        TestDelay delay1 = new TestDelay(System.currentTimeMillis() + 1000*3);
        TestDelay delay2 = new TestDelay(System.currentTimeMillis() + 1000*3);
        list.add(delay1);
        list.add(delay2);
        DelayQueue  delayQueue = new DelayQueue(list);
        for(int i=delayQueue.size();i>0;i--){
            System.out.print(delayQueue.poll(5, TimeUnit.SECONDS));
        }
        // 3s 7ms
        // blocking.TestDelay@69a7c3ef blocking.TestDelay@530b61a
    }

}
package blocking;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * Created by hualei on 2017/8/29.
 */
public class TestDelay implements Delayed{

    private long time;
    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        return 0;
    }

    public TestDelay(long time){
        this.time = time;
    }

}

参考文档地址:(http://www.infoq.com/cn/articles/java-blocking-queue)

你可能感兴趣的:(java)