zoukankan      html  css  js  c++  java
  • 非阻塞式线程安全列表-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
  • 相关阅读:
    Linux文件系统的设计
    HTML中Select的使用具体解释
    【大话设计模式】—— 工厂方法模式
    C++ Primer 学习笔记_84_模板与泛型编程 --模板特化
    Arcgis API for Android之GPS定位
    “大型票务系统”中对机器恶意訪问的处理——验证码
    hdu 4611
    Java实现 蓝桥杯VIP 算法训练 ALGO-85进制转换
    Java实现 蓝桥杯VIP 算法训练 摆动序列
    Java实现 蓝桥杯VIP 算法训练 摆动序列
  • 原文地址:https://www.cnblogs.com/wxgblogs/p/5464413.html
Copyright © 2011-2022 走看看