Java Code Examples for java.util.concurrent.CopyOnWriteArrayList#set()

The following examples show how to use java.util.concurrent.CopyOnWriteArrayList#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testIteratorAndNonStructuralChanges() {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
    Iterator<String> abcde = list.iterator();
    assertEquals("a", abcde.next());
    list.set(1, "B");
    assertEquals("b", abcde.next());
    assertEquals("c", abcde.next());
    assertEquals("d", abcde.next());
    assertEquals("e", abcde.next());
}
 
Example 2
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * The sub list throws on non-structural changes, even though that disagrees
 * with the subList() documentation which suggests that only size-changing
 * operations will trigger ConcurrentModificationException.
 */
public void testSubListAndNonStructuralChanges() {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
    List<String> bcd = list.subList(1, 4);
    list.set(2, "C");
    try {
        bcd.get(1);
        fail();
    } catch (ConcurrentModificationException expected) {
    }
}
 
Example 3
Source File: RandomTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultithreading1() throws Exception {

    final AtomicInteger cnt = new AtomicInteger(0);
    final CopyOnWriteArrayList<float[]> list = new CopyOnWriteArrayList<>();

    Thread[] threads = new Thread[10];
    for (int x = 0; x < threads.length; x++) {
        list.add(null);
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x] = new Thread(new Runnable() {
            @Override
            public void run() {
                Random rnd = Nd4j.getRandom();
                rnd.setSeed(119);
                float[] array = new float[10];

                for (int e = 0; e < array.length; e++) {
                    array[e] = rnd.nextFloat();
                }
                list.set(cnt.getAndIncrement(), array);
            }
        });
        threads[x].start();
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x].join();

        assertNotEquals(null, list.get(x));

        if (x > 0) {
            assertArrayEquals(list.get(0), list.get(x), 1e-5f);
        }
    }
}
 
Example 4
Source File: RandomTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultithreading2() throws Exception {

    final AtomicInteger cnt = new AtomicInteger(0);
    final CopyOnWriteArrayList<INDArray> list = new CopyOnWriteArrayList<>();

    Thread[] threads = new Thread[10];
    for (int x = 0; x < threads.length; x++) {
        list.add(null);
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x] = new Thread(new Runnable() {
            @Override
            public void run() {
                Random rnd = Nd4j.getRandom();
                rnd.setSeed(119);
                INDArray array = Nd4j.getExecutioner().exec(new UniformDistribution(Nd4j.createUninitialized(25)));

                Nd4j.getExecutioner().commit();

                list.set(cnt.getAndIncrement(), array);
            }
        });
        threads[x].start();
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x].join();

        assertNotEquals(null, list.get(x));

        if (x > 0) {
            assertEquals(list.get(0), list.get(x));
        }
    }
}
 
Example 5
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultithreading1() throws Exception {

    final AtomicInteger cnt = new AtomicInteger(0);
    final CopyOnWriteArrayList<float[]> list = new CopyOnWriteArrayList<>();

    Thread[] threads = new Thread[10];
    for (int x = 0; x < threads.length; x++) {
        list.add(null);
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x] = new Thread(new Runnable() {
            @Override
            public void run() {
                Random rnd = Nd4j.getRandom();
                rnd.setSeed(119);
                float[] array = new float[10];

                for (int e = 0; e < array.length; e++) {
                    array[e] = rnd.nextFloat();
                }
                list.set(cnt.getAndIncrement(), array);
            }
        });
        threads[x].start();
    }

    // we want all threads finished before comparing arrays
    for (int x = 0; x < threads.length; x++)
        threads[x].join();

    for (int x = 0; x < threads.length; x++) {
        assertNotEquals(null, list.get(x));

        if (x > 0) {
            assertArrayEquals(list.get(0), list.get(x), 1e-5f);
        }
    }
}
 
Example 6
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultithreading2() throws Exception {

    final AtomicInteger cnt = new AtomicInteger(0);
    final CopyOnWriteArrayList<INDArray> list = new CopyOnWriteArrayList<>();

    Thread[] threads = new Thread[10];
    for (int x = 0; x < threads.length; x++) {
        list.add(null);
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x] = new Thread(new Runnable() {
            @Override
            public void run() {
                Random rnd = Nd4j.getRandom();
                rnd.setSeed(119);
                INDArray array = Nd4j.getExecutioner().exec(new UniformDistribution(Nd4j.createUninitialized(25)));

                Nd4j.getExecutioner().commit();

                list.set(cnt.getAndIncrement(), array);
            }
        });
        threads[x].start();
    }

    // we want all threads finished before comparing arrays
    for (int x = 0; x < threads.length; x++)
        threads[x].join();

    for (int x = 0; x < threads.length; x++) {
        assertNotEquals(null, list.get(x));

        if (x > 0) {
            assertEquals(list.get(0), list.get(x));
        }
    }
}
 
Example 7
Source File: BinaryGapTest.java    From algorithms with MIT License 4 votes vote down vote up
@Test
public void collections() {
    Map<String, Integer> map1 = new HashMap<>();
    map1.values();
    map1.keySet();
    map1.isEmpty();
    map1.clear();
    map1.merge("t", 5, (v1, v2) -> v1 +v2);
    System.out.println(map1.get("t"));
    map1.merge("t", 4, (v1, v2) -> v1 +v2);
    System.out.println(map1.get("t"));
    System.out.println(map1.computeIfAbsent("r", k -> 1));
    System.out.println(map1.get("r"));
    map1.put("", 1);
    System.out.println(map1.computeIfPresent("", (k, v) -> v + 1));
    System.out.println(map1.get(""));
    System.out.println(map1.getOrDefault("", 5));
    map1.merge("", 5, (i, j) -> i + j);
    map1.merge("a", 5, (i, j) -> i + j);
    Set<Map.Entry<String, Integer>> entrySet = map1.entrySet();
    System.out.println(map1.compute("", (k,i) -> i + 1));
    System.out.println(map1.get(""));
    System.out.println(map1.get("a"));
    map1.putIfAbsent("d", 8);
    System.out.println(map1.replace("d", 9));
    System.out.println(map1.get("d"));
    map1.containsKey("d");
    map1.containsValue(8);
    map1.remove("d");

    NavigableMap<Integer, Integer> map2 = new TreeMap<>(Comparator.naturalOrder());
    map2.ceilingEntry(1);
    map2.ceilingKey(1);
    map2.floorEntry(1);
    map2.floorKey(1);
    map2.descendingKeySet();
    map2.descendingMap();
    map2.higherEntry(1);
    map2.higherKey(1);
    map2.lowerEntry(1);
    map2.lowerKey(1);
    map2.navigableKeySet();
    map2.firstEntry();
    map2.lastEntry();
    map2.pollFirstEntry();
    map2.pollLastEntry();

    Set<Integer> set = new HashSet<>();
    set.containsAll(new ArrayList<>());
    set.contains(1);
    set.add(1);
    set.size();
    set.clear();
    set.isEmpty();
    set.iterator();
    set.remove(1);
    NavigableSet<Integer> set1 = new TreeSet<>(Comparator.naturalOrder());
    set1.ceiling(1);
    set1.floor(1);
    set1.higher(1);
    set1.lower(1);
    set1.pollFirst();
    set1.pollLast();

    PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.reverseOrder());
    priorityQueue.add(4);
    priorityQueue.add(2);
    priorityQueue.offer(3);

    int a = priorityQueue.peek();
    int b = priorityQueue.poll();
    priorityQueue.remove(1);

    CopyOnWriteArrayList<Integer> copyList = new CopyOnWriteArrayList<>();
    copyList.addIfAbsent(1);
    AtomicInteger atomicInteger = new AtomicInteger(0);
    int index = atomicInteger.getAndAccumulate(1, (i, j) -> (i+j) % 20);
    copyList.set(index, 2);
    copyList.iterator();
}