非阻塞式线程安全列表-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
posted @ 2016-05-06 09:17  简单爱_wxg  阅读(1083)  评论(0编辑  收藏  举报