• 非阻塞式线程安全列表-ConcurrentLinkedDeque


    一、ConcurrentLinkedDeque

    1. public class ConcurrentLinkedDeque<E>  
    2.     extends AbstractCollection<E>  
    3.     implements Deque<E>, java.io.Serializable 

    二、主要的方法

    • public E pollFirst():返回并移除第一个元素。如果列表为空,抛出NoSuchElementException异常
    • public E pollLast():返回并移除最后一个元素。如果列表为空,抛出NoSuchElementException异常
    • public E poll():返回并移除第一个元素。如果列表为空,抛出NoSuchElementException异常
    • public E getFirst():返回但不移除第一个元素。如果列表为空,抛出NoSuchElementException异常
    • public E getLast():返回但不移除最后一个元素。如果列表为空,抛出NoSuchElementException异常
    • public E peek():返回并移除第一个元素。如果列表为空,抛出NullPointerException异常
    • public E peekFirst():返回并移除第一个元素。如果列表为空,抛出NullPointerException异常
    • public E peekLast():返回并移除最后一个元素。如果列表为空,抛出NullPointerException异常
    • public E removeFirst():返回并移除第一个元素。如果列表为空,抛出NoSuchElementException异常
    • public boolean remove(Object o):返回并移除第一个元素。如果列表为空,抛出NoSuchElementException异常
    • public E removeLast():返回并移除最后一个元素。如果列表为空,抛出NoSuchElementException异常
    public class AddTask implements Runnable {
        private ConcurrentLinkedDeque<String> linkedDeque;
        public AddTask(ConcurrentLinkedDeque<String> linkedDeque) {
            super();
            this.linkedDeque = linkedDeque;
        }
        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            for (int i = 0; i < 10000; i++) {
                linkedDeque.add(name + ": Element " + i);
            }
        }
    }
    public class PollTask implements Runnable {
        private ConcurrentLinkedDeque<String> linkedDeque;
        public PollTask(ConcurrentLinkedDeque<String> linkedDeque) {
            super();
            this.linkedDeque = linkedDeque;
        }
        @Override
        public void run() {
            for (int i = 0; i < 5000; i++) {
                linkedDeque.pollFirst();
                linkedDeque.pollLast();
            }
        }
    }
    public class ConcurrentLinkedDequeMain {
        public static void main(String[] args) {
            ConcurrentLinkedDeque<String> linkedDeque = new ConcurrentLinkedDeque<String>();
            Thread threads[] = new Thread[100];
            for (int i = 0; i < threads.length; i++) {
                AddTask task = new AddTask(linkedDeque);
                threads[i] = new Thread(task);
                threads[i].start();
            }
            System.out.println("Main:"+threads.length+" AddTask Threads has Launched");
            for (int i = 0; i < threads.length; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("Main: Size of the List:" + linkedDeque.size());
    
            Thread threads2[] = new Thread[100];
            for (int i = 0; i < threads2.length; i++) {
                PollTask task = new PollTask(linkedDeque);
                threads2[i] = new Thread(task);
                threads2[i].start();
            }
            System.out.println("Main:" + threads2.length+ " PollTask Threads has Launched");
            for (int i = 0; i < threads2.length; i++) {
                try {
                    threads2[i].join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("Main: Size of the List:" + linkedDeque.size());
        }
    }
    Main:100 AddTask Threads has Launched
    Main: Size of the List:1000000
    Main:100 PollTask Threads has Launched
    Main: Size of the List:0
  • 相关阅读:
    模型驱动自动化测试框架
    TestPartner脚本错误处理的例子
    学习《Selenium 1.0 Testing Tools》
    自动化测试视频【持续更新】
    《软件测试基本功》系列教程
    自动化测试的误解与自动化测试的好处
    广州自动化测试实战训练系列课中的《QTP工具应用实战》课程PPT
    零基础QTP自动化测试训练
    Selenium结合FESTSwing测试Applet
    TestPartner自动化测试培训大纲
  • 原文地址:https://www.cnblogs.com/wxgblogs/p/5464413.html
Copyright © 2020-2023  润新知