Java ConcurrentLinkedDeque class

Description

Java ConcurrentLinkedDeque class

import java.util.concurrent.ConcurrentLinkedDeque;

class PollTask implements Runnable {
  private ConcurrentLinkedDeque<String> list;
  public PollTask(ConcurrentLinkedDeque<String> list) {
    this.list = list;
  }/*from www  .jav a 2  s  .  c om*/
  @Override
  public void run() {
    for (int i = 0; i < 50; i++) {
      list.pollFirst();
      list.pollLast();
    }
  }
}
class AddTask implements Runnable {
  private ConcurrentLinkedDeque<String> list;
  public AddTask(ConcurrentLinkedDeque<String> list) {
    this.list = list;
  }
  @Override
  public void run() {
    String name = Thread.currentThread().getName();
    for (int i = 0; i < 100; i++) {
      list.add(name + ": Element " + i);
    }
  }
}

public class Main {
  public static void main(String[] args) throws Exception {
    ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<>();
    Thread threads[] = new Thread[100];
    for (int i = 0; i < threads.length; i++) {
      AddTask task = new AddTask(list);
      threads[i] = new Thread(task);
      threads[i].start();
    }
    System.out.println("launched"+ threads.length);

    for (int i = 0; i < threads.length; i++) {
      threads[i].join();
    }
    System.out.println("Size of the List: "+ list.size());
    for (int i = 0; i < threads.length; i++) {
      PollTask task = new PollTask(list);
      threads[i] = new Thread(task);
      threads[i].start();
    }
    System.out.println("launched"+ threads.length);
    for (int i = 0; i < threads.length; i++) {
      threads[i].join();
    }
    System.out.println("Size of the List: "+ list.size());
  }
}



PreviousNext

Related