Follow these steps to implement the example:
- Create a class named AddTask and specify that it implements the Runnable interface:
public class AddTask implements Runnable {
- Declare a private ConcurrentLinkedDeque attribute parameterized by the String class named list:
private final ConcurrentLinkedDeque<String> list;
- Implement the constructor of the class to initialize its attribute:
public AddTask(ConcurrentLinkedDeque<String> list) {
this.list=list;
}
- 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);
}
}
- Create a class named PollTask and specify that it implements the Runnable interface:
public class PollTask implements Runnable {
- Declare a private ConcurrentLinkedDeque attribute parameterized by the String class named list:
private final ConcurrentLinkedDeque<String> list;
- Implement the constructor of the class to initialize its attribute:
public PollTask(ConcurrentLinkedDeque<String> list) {
this.list=list;
}
- 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();
}
}
- 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) {
- Create a ConcurrentLinkedDeque object parameterized by the String class named list:
ConcurrentLinkedDeque<String> list=new ConcurrentLinkedDeque<>();
- Create an array of 100 Thread objects named threads:
Thread threads[]=new Thread[100];
- 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);
- 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();
}
}
- Write the size of the list in the console:
System.out.printf("Main: Size of the List: %d ",list.size());
- 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);
- 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();
}
}
- Write the size of the list in the console:
System.out.printf("Main: Size of the List: %d ",list.size());