Java CopyOnWriteArrayList update a List from multiple Threads

Description

Java CopyOnWriteArrayList update a List from multiple Threads

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

public class Main {
   Random random = new Random();

   Thread updatingThread;/*www  .j a v a  2s . c o  m*/
   private void start() {
      System.out.println("Using CopyOnWrite");
      copyOnWriteSolution();
      System.out.println("Using SynchronizedList");
      synchronizedListSolution();
   }

   private void synchronizedListSolution() {
      final List<String> list = Collections.synchronizedList(new ArrayList<String>());
      startUpdatingThread(list);
      synchronized (list) {
         for (String element : list) {
            System.out.println("Element :" + element);
         }
      }
      stopUpdatingThread();
   }

   private void copyOnWriteSolution() {
      CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
      startUpdatingThread(list);
      for (String element : list) {
         System.out.println("Element :" + element);
      }
      stopUpdatingThread();
   }

   private void stopUpdatingThread() {
      updatingThread.interrupt();
   }
   private void startUpdatingThread(final List<String> list) {
      updatingThread = new Thread(new Runnable() {
         long counter = 0;

         public void run() {
            while (!Thread.interrupted()) {
               int size = list.size();
               if (random.nextBoolean()) {
                  if (size > 1) {
                     list.remove(random.nextInt(size - 1));
                  }
               } else {
                  if (size < 20) {
                     list.add("Random string " + counter);
                  }
               }
               counter++;
            }
         }
      });
      updatingThread.start();
      // let it warm up for a second
      try {
         Thread.sleep(100);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }

   }
   public static void main(String[] args) {
      new Main().start();
   }

}



PreviousNext

Related