Use CopyOnWriteArrayList : CopyOnWriteArrayList « JDK 7 « Java






Use CopyOnWriteArrayList


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

public class Test {
  public static void main(String[] args) {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    startUpdatingThread(list);
    for (String element : list) {
      System.out.println("Element :" + element);
    }
    updatingThread.interrupt();

    List<String> lista = Collections.synchronizedList(new ArrayList<String>());
    startUpdatingThread(lista);
    synchronized (lista) {
      for (String element : lista) {
        System.out.println("Element :" + element);
      }
    }
    updatingThread.interrupt();
  }

  static Thread updatingThread;

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

      public void run() {
        while (!Thread.interrupted()) {
          int size = list.size();
          Random random = new Random();
          if (random.nextBoolean()) {
            if (size > 1) {
              list.remove(random.nextInt(size - 1));
            }
          } else {
            if (size < 20) {
              list.add("Random string " + counter);
            }
          }
          counter++;
        }
      }

    });
    updatingThread.start();
  }
}

 








Related examples in the same category