
- Java 併發教程
- 併發 - 首頁
- 併發 - 概述
- 併發 - 環境設定
- 併發 - 主要操作
- 執行緒間通訊
- 併發 - 同步
- 併發 - 死鎖
- 工具類示例
- 併發 - ThreadLocal
- 併發 - ThreadLocalRandom
- 鎖示例
- 併發 - Lock
- 併發 - ReadWriteLock
- 併發 - Condition
- 原子變數示例
- 併發 - AtomicInteger
- 併發 - AtomicLong
- 併發 - AtomicBoolean
- 併發 - AtomicReference
- 併發 - AtomicIntegerArray
- 併發 - AtomicLongArray
- 併發 - AtomicReferenceArray
- Executor 示例
- 併發 - Executor
- 併發 - ExecutorService
- ScheduledExecutorService
- 執行緒池示例
- 併發 - newFixedThreadPool
- 併發 - newCachedThreadPool
- newScheduledThreadPool
- newSingleThreadExecutor
- 併發 - ThreadPoolExecutor
- ScheduledThreadPoolExecutor
- 高階示例
- 併發 - Futures 和 Callables
- 併發 - Fork-Join 框架
- 併發集合
- 併發 - BlockingQueue
- 併發 - ConcurrentMap
- ConcurrentNavigableMap
- 併發有用資源
- 併發 - 快速指南
- 併發 - 有用資源
- 併發 - 討論
Java 併發 - ConcurrentMap 介面
java.util.concurrent.ConcurrentMap 介面是 Map 介面的子介面,支援對底層 map 變數進行原子操作。它具有 get 和 set 方法,其工作方式類似於對 volatile 變數的讀寫。也就是說,set 與隨後對同一變數的任何 get 之間存在 happens-before 關係。此介面確保執行緒安全和原子性保證。
ConcurrentMap 方法
序號 | 方法及描述 |
---|---|
1 | default V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) 嘗試為指定的鍵及其當前對映值(如果當前沒有對映則為 null)計算對映。 |
2 | default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) 如果指定的鍵尚未與值關聯(或對映到 null),則嘗試使用給定的對映函式計算其值,並將其輸入到此對映中,除非為 null。 |
3 | default V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) 如果指定鍵的值存在且不為 null,則嘗試根據鍵及其當前對映值計算新的對映。 |
4 | default void forEach(BiConsumer<? super K,? super V> action) 對該對映中的每個條目執行給定的操作,直到所有條目都已處理或操作丟擲異常。 |
5 | default V getOrDefault(Object key, V defaultValue) 返回指定鍵對映到的值,如果此對映不包含該鍵的對映,則返回 defaultValue。 |
6 | default V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) 如果指定的鍵尚未與值關聯或與 null 關聯,則將其與給定的非 null 值關聯。 |
7 | V putIfAbsent(K key, V value) 如果指定的鍵尚未與值關聯,則將其與給定的值關聯。 |
8 | boolean remove(Object key, Object value) 僅噹噹前對映到給定值時,才刪除鍵的條目。 |
9 | V replace(K key, V value) 僅噹噹前對映到某個值時,才替換鍵的條目。 |
10 | boolean replace(K key, V oldValue, V newValue) 僅噹噹前對映到給定值時,才替換鍵的條目。 |
11 | default void replaceAll(BiFunction<? super K,? super V,? extends V> function) 將每個條目的值替換為對該條目呼叫給定函式的結果,直到所有條目都已處理或函式丟擲異常。 |
示例
以下 TestThread 程式顯示了 ConcurrentMap 與 HashMap 的用法。
import java.util.ConcurrentModificationException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class TestThread { public static void main(final String[] arguments) { Map<String,String> map = new ConcurrentHashMap<String, String>(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("5", "Five"); map.put("6", "Six"); System.out.println("Initial ConcurrentHashMap: " + map); Iterator<String> iterator = map.keySet().iterator(); try { while(iterator.hasNext()) { String key = iterator.next(); if(key.equals("3")) { map.put("4", "Four"); } } } catch(ConcurrentModificationException cme) { cme.printStackTrace(); } System.out.println("ConcurrentHashMap after modification: " + map); map = new HashMap<String, String>(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("5", "Five"); map.put("6", "Six"); System.out.println("Initial HashMap: " + map); iterator = map.keySet().iterator(); try { while(iterator.hasNext()) { String key = iterator.next(); if(key.equals("3")) { map.put("4", "Four"); } } System.out.println("HashMap after modification: " + map); } catch(ConcurrentModificationException cme) { cme.printStackTrace(); } } }
這將產生以下結果。
輸出
Initial ConcurrentHashMap: {1 = One, 2 = Two, 3 = Three, 5 = Five, 6 = Six} ConcurrentHashMap after modification: {1 = One, 2 = Two, 3 = Three, 4 = Four, 5 = Five, 6 = Six} Initial HashMap: {1 = One, 2 = Two, 3 = Three, 5 = Five, 6 = Six} java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextNode(Unknown Source) at java.util.HashMap$KeyIterator.next(Unknown Source) at TestThread.main(TestThread.java:48)