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
  • 相关阅读:
    将图片制作数据拒
    python中PIL.Image,OpenCV,Numpy图像格式相互转换
    9、【Hive】初始化元数据库时失败,遇到org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent
    8、【Hive Mysql】MySQL8 提示Public Key Retrieval is not allowed错误解决方法
    7.【Hive】hive启动出现权限错误 /tmp/hive on HDFS should be writable
    一、Zookeeper简明笔记
    6、System times on machines may be out of sync. Check system time and time zones
    5、利用搭建的hadoop集群计算wordcount时出现内存问题
    4、利用hadoop自带的mr 示例测试运行任务失败: Could not find or load main class org.apache.hadoop.mapreduce.v2.app.MRAppMaster
    3、hadoop运行jar包报错 : "/bin/bash: /bin/java: No such file or directory"
  • 原文地址:https://www.cnblogs.com/wxgblogs/p/5464413.html
Copyright © 2011-2022 走看看