Example usage for java.util Collections synchronizedList

List of usage examples for java.util Collections synchronizedList

Introduction

In this page you can find the example usage for java.util Collections synchronizedList.

Prototype

public static <T> List<T> synchronizedList(List<T> list) 

Source Link

Document

Returns a synchronized (thread-safe) list backed by the specified list.

Usage

From source file:Main.java

public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
    List list = Collections.synchronizedList(arrayList);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> syncList = Collections.synchronizedList(new ArrayList<String>());

    syncList.add("one");
    syncList.add("two");
    syncList.add("three");

    synchronized (syncList) {
        Iterator<String> iterator = syncList.iterator();
        while (iterator.hasNext()) {
            System.out.println("item: " + iterator.next());
        }//  w ww .j av  a 2  s .  c  o m
    }
}

From source file:SafeCollectionIteration.java

public static void main(String[] args) {
    List wordList = Collections.synchronizedList(new ArrayList());

    wordList.add("Iterators");
    wordList.add("require");
    wordList.add("special");
    wordList.add("handling");

    synchronized (wordList) {
        Iterator iter = wordList.iterator();
        while (iter.hasNext()) {
            String s = (String) iter.next();
            System.out.println("found string: " + s + ", length=" + s.length());
        }/*  w  w w  .j  a v a 2  s.  co  m*/
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    LinkedList queue = new LinkedList();
    Object object = "";
    queue.add(object);//from   w w w  .j a va  2 s  . c o m

    Object o = queue.removeFirst();

    queue = (LinkedList) Collections.synchronizedList(queue);
}

From source file:Main.java

public static void main(String[] args) {
    Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
    List<String> list = Collections.synchronizedList(new ArrayList<String>());
    Set<String> s = Collections.synchronizedSet(new HashSet<String>());
    Map<String, String> m = Collections.synchronizedMap(new HashMap<String, String>());
}

From source file:Test.java

public static void main(String[] args) {
    final AtomicLong orderIdGenerator = new AtomicLong(0);
    final List<Item> orders = Collections.synchronizedList(new ArrayList<Item>());

    for (int i = 0; i < 10; i++) {
        Thread orderCreationThread = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    long orderId = orderIdGenerator.incrementAndGet();
                    Item order = new Item(Thread.currentThread().getName(), orderId);
                    orders.add(order);//from   w w  w.j av a 2s. co m
                }
            }
        });
        orderCreationThread.setName("Order Creation Thread " + i);
        orderCreationThread.start();
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Set<Long> orderIds = new HashSet<Long>();
    for (Item order : orders) {
        orderIds.add(order.getID());
        System.out.println("Order id:" + order.getID());
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();

    // populate the list
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("from java2s.com");

    // create a synchronized list
    List<String> synlist = Collections.synchronizedList(list);

    System.out.println("Sunchronized list is :" + synlist);
}

From source file:SafeListCopy.java

public static void main(String[] args) {
    List wordList = Collections.synchronizedList(new ArrayList());

    wordList.add("Synchronization");
    wordList.add("is");
    wordList.add("important");

    String[] wordA = (String[]) wordList.toArray(new String[0]);

    printWords(wordA);//from  w  ww .j  a v a 2  s  .c o m

    String[] wordB;
    synchronized (wordList) {
        int size = wordList.size();
        wordB = new String[size];
        wordList.toArray(wordB);
    }

    printWords(wordB);

    // Third technique (the 'synchronized' *is* necessary)
    String[] wordC;
    synchronized (wordList) {
        wordC = (String[]) wordList.toArray(new String[wordList.size()]);
    }

    printWords(wordC);
}

From source file:CollectionTest.java

public static void main(String[] args) {

    nLoops = 10000;/*from   www . j a  v  a2 s.  c  o m*/
    doTest(new Vector());
    doTest(new ArrayList());
    doTest(Collections.synchronizedList(new ArrayList()));
    nLoops = Integer.parseInt(args[0]);

    System.out.println("Starting synchronized vector test");
    cleanGC();
    Timestamp syncTS = new Timestamp();
    doTest(new Vector());
    syncTS.stop();
    System.out.println("Synchronized vector took " + syncTS);

    System.out.println("Starting unsynchronized vector test");
    cleanGC();
    Timestamp unsyncTS = new Timestamp();
    unsyncTS.stop();
    System.out.println("Unsynchronized vector took " + unsyncTS);

    double d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime())) / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " " + syncTS.units() + " per call");

    System.out.println("Starting synchronized array list test");
    cleanGC();
    syncTS = new Timestamp();
    doTest(Collections.synchronizedList(new ArrayList()));
    syncTS.stop();
    System.out.println("Synchronized array list took " + syncTS);

    System.out.println("Starting unsynchronized array list test");
    cleanGC();
    unsyncTS = new Timestamp();
    doTest(new ArrayList());
    unsyncTS.stop();
    System.out.println("Unsynchronized aray list took " + unsyncTS);

    d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime())) / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " " + syncTS.units() + " per call");
}

From source file:Test.java

public static void main(String[] args) {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    startUpdatingThread(list);/*from   w ww .j  a v  a 2s .  c o m*/
    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();
}