ConcurrentHashMap in Java with Examples - Java 147

ConcurrentHashMap in Java with Examples – Java 147

ConcurrentHashMap in Java with Examples

是執行緒安全的,即多個執行緒可以對單一物件進行操作,而不會出現任何複雜情況,任意數量的執行緒都可以執行檢索操作,但是要更新對象,必須鎖定該執行緒想要操作的特定區段,預設並發等級是 16 ,執行緒一次可以執行 16 個更新操作, ConcurrentHashMap in Java 介紹常見的 entrySet 、 forEach 、 toArray 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- map
       |                   +- concurrenthashmap
       |                       +- ConcurrentHashMapWithExamplesTest.java  

單元測試

ConcurrentHashMap Java 提供循環訪問、轉成陣列等操作 Map 中的元素。

Fruit

建立 Fruit 物件,覆寫 equals 、 hashCode ,定義屬性和方法,用來建立一個物件。

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	public static class Fruit {
		private String name;
		private double quantity;
		private int type;

		public Fruit(String name, double quantity, int type) {
			this.name = name;
			this.quantity = quantity;
			this.type = type;
		}

		public String toString() {
			ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
			builder.appendSuper(super.toString());
			builder.append("name", name);
			builder.append("quantity", quantity);
			builder.append("type", type);
			return builder.toString();
		}

		public boolean equals(Object object) {
			if (!(object instanceof Fruit)) {
				return false;
			}
			if (this == object) {
				return true;
			}
			Fruit other = (Fruit) object;
			return new EqualsBuilder().append(getName(), other.getName()).isEquals();
		}

		public int hashCode() {
			return new HashCodeBuilder().append(getName()).toHashCode();
		}
	}

iteratorThrowException

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

	@Test
	public void iteratorThrowException() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Map<String, Fruit> map = new HashMap<>();
			map.put("Grape", new Fruit("Grape", -1, 1));
			map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
			map.put("Lemon", new Fruit("Lemon", 1, 3));
			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			});

			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			});
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
[11] Grape
[13] Grape
[11] Kiwifruit
[12] Grape
[15] Grape
[14] Grape
[12] Kiwifruit
[15] Kiwifruit
[14] Kiwifruit
[15] Lemon
[11] Lemon
[13] Kiwifruit
[14] Lemon
[12] Lemon
Exception in thread "pool-1-thread-3" 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.map.concurrenthashmap.ConcurrentHashMapWithExamplesTest.lambda$1(ConcurrentHashMapWithExamplesTest.java:81)

iterator

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

	@Test
	public void iterator() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Map<String, Fruit> map = new ConcurrentHashMap<>();
			map.put("Grape", new Fruit("Grape", -1, 1));
			map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
			map.put("Lemon", new Fruit("Lemon", 1, 3));
			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			});

			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			});
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
[14] Grape
[15] Grape
[11] Grape
[12] Grape
[11] Kiwifruit
[13] Grape
[12] Kiwifruit
[11] Lemon
[12] Lemon
[15] Kiwifruit
[14] Kiwifruit
[13] Kiwifruit
[14] Lemon
[13] Lemon
[15] Lemon

entrySet

ConcurrentHashMap Java 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得元素。

	@Test
	public void entrySet() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		for (Map.Entry<String, Fruit> e : map.entrySet()) {
			System.out.println(e.getKey() + ", " + e.getValue());
		}
	}
Grape, {"name":"Grape","quantity":-1.0,"type":1}
Kiwifruit, {"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}
Lemon, {"name":"Lemon","quantity":1.0,"type":3}

forEach

ConcurrentHashMap Java 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得元素。

	@Test
	public void forEach() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		map.forEach((k, v) -> System.out.println(k + ", " + v));
	}
Grape, {"name":"Grape","quantity":-1.0,"type":1}
Kiwifruit, {"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}
Lemon, {"name":"Lemon","quantity":1.0,"type":3}

keyForEach

ConcurrentHashMap Java 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得 key 元素。

	@Test
	public void keyForEach() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		map.keySet().forEach(e -> System.out.println(e));
	}
Grape
Kiwifruit
Lemon

keyForEachRemaining

ConcurrentHashMap Java 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得 key 剩餘元素。

	@Test
	public void keyForEachRemaining() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Set<String> set = map.keySet();
		Iterator<String> it = set.iterator();
		int i = 0;
		while (it.hasNext()) {
			System.out.println(it.next());
			if (i == 1) {
				break;
			}
			i++;
		}
		System.out.println("----------");
		it.forEachRemaining(e -> {
			System.out.println(e);
		});
	}
Grape
Kiwifruit
----------
Lemon

keyIterator

Java ConcurrentHashMap 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得 key 元素。

	@Test
	public void keyIterator() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
Grape
Kiwifruit
Lemon

valueForEach

Java ConcurrentHashMap 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得 value 元素。

	@Test
	public void valueForEach() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		map.values().forEach(System.out::println);
	}
{"name":"Grape","quantity":-1.0,"type":1}
{"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}
{"name":"Lemon","quantity":1.0,"type":3}

valueForEachRemaining

Java ConcurrentHashMap 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得 value 剩餘元素。

	@Test
	public void valueForEachRemaining() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Collection<Fruit> collection = map.values();
		Iterator<Fruit> it = collection.iterator();
		int i = 0;
		while (it.hasNext()) {
			System.out.println(it.next());
			if (i == 1) {
				break;
			}
			i++;
		}
		System.out.println("----------");
		it.forEachRemaining(e -> {
			System.out.println(e);
		});
	}
{"name":"Grape","quantity":-1.0,"type":1}
{"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}
----------
{"name":"Lemon","quantity":1.0,"type":3}

valueIterator

Java ConcurrentHashMap 建立一個 ConcurrentHashMap ,內有三個元素,迴圈取得 value 元素。

	@Test
	public void valueIterator() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Iterator<Fruit> it = map.values().iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
{"name":"Grape","quantity":-1.0,"type":1}
{"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}
{"name":"Lemon","quantity":1.0,"type":3}

keyToArray

Java ConcurrentHashMap 建立一個 ConcurrentHashMap ,內有三個元素, key 轉換成 String 陣列, Java ConcurrentHashMap Example 提供範例參考。

	@Test
	public void keyToArray() {
		int expectedSize = 3;
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));

		String[] array = new String[map.size()];
		map.keySet().toArray(array);
		for (String e : array) {
			System.out.println(e);
		}
		assertEquals(expectedSize, array.length);
	}
Grape
Kiwifruit
Lemon

keyStreamToArray

Java ConcurrentHashMap 建立一個 ConcurrentHashMap ,內有三個元素,使用 stream , key 轉換成 String 陣列, Java ConcurrentHashMap Example 提供範例參考。

	@Test
	public void keyStreamToArray() {
		int expectedSize = 3;
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));

		String[] array = map.keySet().stream().toArray(String[]::new);
		for (String e : array) {
			System.out.println(e);
		}
		assertEquals(expectedSize, array.length);
	}
Grape
Kiwifruit
Lemon

ConcurrentHashMapWithExamplesTest.java

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

package org.ruoxue.java_147.map.concurrenthashmap;

import static org.junit.Assert.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class ConcurrentHashMapWithExamplesTest {

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	public static class Fruit {
		private String name;
		private double quantity;
		private int type;

		public Fruit(String name, double quantity, int type) {
			this.name = name;
			this.quantity = quantity;
			this.type = type;
		}

		public String toString() {
			ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
			builder.appendSuper(super.toString());
			builder.append("name", name);
			builder.append("quantity", quantity);
			builder.append("type", type);
			return builder.toString();
		}

		public boolean equals(Object object) {
			if (!(object instanceof Fruit)) {
				return false;
			}
			if (this == object) {
				return true;
			}
			Fruit other = (Fruit) object;
			return new EqualsBuilder().append(getName(), other.getName()).isEquals();
		}

		public int hashCode() {
			return new HashCodeBuilder().append(getName()).toHashCode();
		}
	}

	@Test
	public void iteratorThrowException() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Map<String, Fruit> map = new HashMap<>();
			map.put("Grape", new Fruit("Grape", -1, 1));
			map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
			map.put("Lemon", new Fruit("Lemon", 1, 3));
			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			});

			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			});
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Test
	public void iterator() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Map<String, Fruit> map = new ConcurrentHashMap<>();
			map.put("Grape", new Fruit("Grape", -1, 1));
			map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
			map.put("Lemon", new Fruit("Lemon", 1, 3));
			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			});

			IntStream.range(0, poolSize).forEach(e -> {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			});
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Test
	public void entrySet() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		for (Map.Entry<String, Fruit> e : map.entrySet()) {
			System.out.println(e.getKey() + ", " + e.getValue());
		}
	}

	@Test
	public void forEach() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		map.forEach((k, v) -> System.out.println(k + ", " + v));
	}

	@Test
	public void keyForEach() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		map.keySet().forEach(e -> System.out.println(e));
	}

	@Test
	public void keyForEachRemaining() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Set<String> set = map.keySet();
		Iterator<String> it = set.iterator();
		int i = 0;
		while (it.hasNext()) {
			System.out.println(it.next());
			if (i == 1) {
				break;
			}
			i++;
		}
		System.out.println("----------");
		it.forEachRemaining(e -> {
			System.out.println(e);
		});
	}

	@Test
	public void keyIterator() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}

	@Test
	public void valueForEach() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		map.values().forEach(System.out::println);
	}

	@Test
	public void valueForEachRemaining() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Collection<Fruit> collection = map.values();
		Iterator<Fruit> it = collection.iterator();
		int i = 0;
		while (it.hasNext()) {
			System.out.println(it.next());
			if (i == 1) {
				break;
			}
			i++;
		}
		System.out.println("----------");
		it.forEachRemaining(e -> {
			System.out.println(e);
		});
	}

	@Test
	public void valueIterator() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));
		Iterator<Fruit> it = map.values().iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}

	@Test
	public void keyToArray() {
		int expectedSize = 3;
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));

		String[] array = new String[map.size()];
		map.keySet().toArray(array);
		for (String e : array) {
			System.out.println(e);
		}
		assertEquals(expectedSize, array.length);
	}

	@Test
	public void keyStreamToArray() {
		int expectedSize = 3;
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		map.put("Lemon", new Fruit("Lemon", 1, 3));

		String[] array = map.keySet().stream().toArray(String[]::new);
		for (String e : array) {
			System.out.println(e);
		}
		assertEquals(expectedSize, array.length);
	}
}

心得分享

Java ConcurrentHashMap Example 可供多個線程同時使用,使其成為需要處理並發存取資料的理想選擇,與鎖定整個資料結構的其他同步機制不同,使用細粒度鎖定僅鎖定集合中被修改的部分,即使在大並發存取下也能夠實現高效能,提供了幾種 ConcurrentHashMap 常見方法的操作範例,在應用上相當廣泛,熟悉 Java ConcurrentHashMap 這些方法的操作,像是: entrySet 、 forEach 、 toArray 等方法。

發佈留言