How to do it...

Follow these steps to implement the example:

  1. Create a class named AddTask and specify that it implements the Runnable interface:
        public class AddTask implements Runnable {
  1. Declare a private ConcurrentLinkedDeque attribute parameterized by the String class named list:
        private final ConcurrentLinkedDeque<String> list;
  1. Implement the constructor of the class to initialize its attribute:
        public AddTask(ConcurrentLinkedDeque<String> list) { 
this.list=list;
}
  1. Implement the run() method of the class. This method will have a loop with 5000 cycles. In each cycle, we will take the first and last elements of the deque so we will take a total of 10,000 elements:
        @Override 
public void run() {
String name=Thread.currentThread().getName();
for (int i=0; i<10000; i++){
list.add(name+": Element "+i);
}
}
  1. Create a class named PollTask and specify that it implements the Runnable interface:
        public class PollTask implements Runnable {
  1. Declare a private ConcurrentLinkedDeque attribute parameterized by the String class named list:
        private final ConcurrentLinkedDeque<String> list;
  1. Implement the constructor of the class to initialize its attribute:
        public PollTask(ConcurrentLinkedDeque<String> list) { 
this.list=list;
}
  1. Implement the run() method of the class. It takes out 10,000 elements of the deque in a loop with 5,000 steps, taking off two elements in each step:
        @Override 
public void run() {
for (int i=0; i<5000; i++) {
list.pollFirst();
list.pollLast();
}
}
  1. Implement the main class of the example by creating a class named Main and adding the main() method to it:
        public class Main { 

public static void main(String[] args) {
  1. Create a ConcurrentLinkedDeque object parameterized by the String class named list:
        ConcurrentLinkedDeque<String> list=new ConcurrentLinkedDeque<>();
  1. Create an array of 100 Thread objects named threads:
        Thread threads[]=new Thread[100];
  1. Create 100 AddTask objects and threads to run each one of them. Store every thread in the array created earlier and start them:
        for (int i=0; i<threads.length ; i++){ 
AddTask task=new AddTask(list);
threads[i]=new Thread(task);
threads[i].start();
}
System.out.printf("Main: %d AddTask threads have been launched ",
threads.length);
  1. Wait for the completion of the threads using the join() method:
        for (int i=0; i<threads.length; i++) { 
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Write the size of the list in the console:
        System.out.printf("Main: Size of the List: %d
",list.size());
  1. Create 100 PollTask objects and threads to run each one of them. Store every thread in the array created earlier and start them:
        for (int i=0; i< threads.length; i++){ 
PollTask task=new PollTask(list);
threads[i]=new Thread(task);
threads[i].start();
}
System.out.printf("Main: %d PollTask threads have been launched ",
threads.length);
  1. Wait for the finalization of the threads using the join() method:
        for (int i=0; i<threads.length; i++) { 
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Write the size of the list in the console:
        System.out.printf("Main: Size of the List: %d
",list.size());
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.86.18