Java CopyOnWriteArraySet Methods - Java 147

Java CopyOnWriteArraySet Methods – Java 147

Java CopyOnWriteArraySet Methods

適用於集合大小通常保持很小,讀取操作遠多於寫入操作,需要在遍歷期間防止執行緒間的衝突,是執行緒安全的,因為通常需要複製整個陣列,所以可變操作 add 或 remove 時的對資源的開銷很大,適用於即時性低的場景,因為有可能會讀到舊的資料, CopyOnWriteArraySet Java Methods 介紹常見的 add 、 remove 、 clear 、 size 等方法,了解陣列 Set 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- set
       |                   +- copyonwritearrayset
       |                       +- CopyOnWriteArraySetMethodsTest.java  

單元測試

CopyOnWriteArraySet Methods Java 提供新增、刪除等操作 Set 中的元素。

readWriteThrowException

建立一個 HashSet ,增加三個元素,多執行緒進行讀取元素時,同時寫入元素,會拋出例外。

	@Test
	public void readWriteThrowException() {
		try {
			int poolSize = 3;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Set<String> set = new HashSet<>();
			set.add("Longan");
			set.add("Tomato");
			set.add("Pear");
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = set.iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					set.add("Grape");
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
[12] Pear
[13] Pear
[12] Longan
[11] Pear
[12] Tomato
[13] Longan
[11] Longan
Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-1" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
	at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
	at org.ruoxue.java_147.set.copyonwritearrayset.CopyOnWriteArraySetMethodsTest.lambda$0(CopyOnWriteArraySetMethodsTest.java:31)

readWrite

CopyOnWriteArraySet Methods Java 建立一個 CopyOnWriteArraySet ,增加三個元素,多執行緒進行讀寫元素時,能夠安全地遍歷集合。

	@Test
	public void readWrite() {
		try {
			int poolSize = 3;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Set<String> set = new CopyOnWriteArraySet<>();
			set.add("Longan");
			set.add("Tomato");
			set.add("Pear");
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = set.iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					set.add("Grape");
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
[13] Longan
[11] Longan
[13] Tomato
[12] Longan
[11] Tomato
[12] Tomato
[11] Pear
[12] Pear
[13] Pear

add

CopyOnWriteArraySet Methods Java 建立一個 CopyOnWriteArraySet ,增加三個元素。

	@Test
	public void add() {
		int expectedSize = 3;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[Longan, Tomato, Pear]

addAll

CopyOnWriteArraySet Methods in Java 建立兩個 CopyOnWriteArraySet ,內各有三個元素,合併成為一個 Set 。

	@Test
	public void addAll() {
		int expectedSize = 6;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new CopyOnWriteArraySet<>();
		set2.add("Grape");
		set2.add("Lemon");
		set2.add("Mango");

		set.addAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[Longan, Tomato, Pear, Grape, Lemon, Mango]

remove

CopyOnWriteArraySet Functions in Java 建立一個 CopyOnWriteArraySet ,內有三個元素,刪除指定元素。

	@Test
	public void remove() {
		int expectedSize = 2;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.remove("Longan");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[Tomato, Pear]

removeAll

CopyOnWriteArraySet Functions in Java 建立一個 CopyOnWriteArraySet ,內有三個元素,刪除來自另一個 Set 中的元素。

	@Test
	public void removeAll() {
		int expectedSize = 1;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new CopyOnWriteArraySet<>();
		set2.add("Longan");
		set2.add("Tomato");
		set2.add("Mango");
		set.removeAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[Pear]

clear

CopyOnWriteArraySet Functions in Java 建立一個 CopyOnWriteArraySet ,內有三個元素,刪除所有元素。

	@Test
	public void clear() {
		int expectedSize = 0;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.clear();
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[]	

size

建立一個 CopyOnWriteArraySet ,內有三個元素,取得集合大小。

	@Test
	public void size() {
		int expectedSize = 3;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.size());
		assertEquals(expectedSize, set.size());
	}
3

isEmpty

建立一個 CopyOnWriteArraySet ,檢查是否為空 Set 。

	@Test
	public void isEmpty() {
		Set<String> set = new CopyOnWriteArraySet<>();
		System.out.println(set.isEmpty());
		assertTrue(set.isEmpty());
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.isEmpty());
		assertFalse(set.isEmpty());
	}
true
false

CopyOnWriteArraySetMethodsTest.java

CopyOnWriteArraySet Methods in Java 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.set.copyonwritearrayset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Test;

public class CopyOnWriteArraySetMethodsTest {

	@Test
	public void readWriteThrowException() {
		try {
			int poolSize = 3;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Set<String> set = new HashSet<>();
			set.add("Longan");
			set.add("Tomato");
			set.add("Pear");
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = set.iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					set.add("Grape");
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Test
	public void readWrite() {
		try {
			int poolSize = 3;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Set<String> set = new CopyOnWriteArraySet<>();
			set.add("Longan");
			set.add("Tomato");
			set.add("Pear");
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = set.iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					set.add("Grape");
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Test
	public void add() {
		int expectedSize = 3;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void addAll() {
		int expectedSize = 6;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new CopyOnWriteArraySet<>();
		set2.add("Grape");
		set2.add("Lemon");
		set2.add("Mango");

		set.addAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void remove() {
		int expectedSize = 2;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.remove("Longan");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void removeAll() {
		int expectedSize = 1;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new CopyOnWriteArraySet<>();
		set2.add("Longan");
		set2.add("Tomato");
		set2.add("Mango");
		set.removeAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void clear() {
		int expectedSize = 0;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.clear();
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void size() {
		int expectedSize = 3;
		Set<String> set = new CopyOnWriteArraySet<>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.size());
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void isEmpty() {
		Set<String> set = new CopyOnWriteArraySet<>();
		System.out.println(set.isEmpty());
		assertTrue(set.isEmpty());
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.isEmpty());
		assertFalse(set.isEmpty());
	}
}

心得分享

CopyOnWriteArraySet Functions in Java 不在原有記憶體區塊中進行寫入操作,而是將記憶體拷貝一份,在新的記憶體中進行寫操作,寫完之後,利用加鎖保證同步,將指針指向新的記憶體,原來的記憶體就可以被回收掉,這是一種用於程式設計中的最佳化策略,是一種延時懶惰策略,提供了幾種 CopyOnWriteArraySet 常見方法的操作範例,在應用上相當廣泛,熟悉 CopyOnWriteArraySet Methods in Java 這些方法的操作,可以快速撰寫程式,降低錯誤率。

發佈留言