-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5831c9b
commit 47852f6
Showing
9 changed files
with
646 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/xyz/xuminghai/completion/CompletableFutureDemo01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package xyz.xuminghai.completion; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* 2023/10/23 12:51 星期一<br/> | ||
* 可能异步计算的一个阶段,当另一个 CompletionStage 完成时执行操作或计算值。 | ||
* 阶段在其计算终止时完成,但这可能反过来触发其他依赖阶段。 | ||
* 简化多线程编程的复杂性 | ||
* | ||
* @author xuMingHai | ||
*/ | ||
public class CompletableFutureDemo01 { | ||
|
||
/** | ||
* 日志记录器 | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CompletableFutureDemo01.class); | ||
|
||
public static void main(String[] args) { | ||
String string; | ||
try { | ||
// 异步分阶段任务 | ||
string = CompletableFuture.runAsync(() -> LOGGER.info(Thread.currentThread().getName())) | ||
.thenRunAsync(() -> LOGGER.info("异步执行任务")) | ||
.thenRun(() -> LOGGER.info("任务完成")) | ||
.thenCombine(CompletableFuture.completedFuture("Ok"), (unused, s) -> s) | ||
.get(); | ||
} | ||
catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
LOGGER.info(string); | ||
|
||
// 串行任务 | ||
CompletableFuture.completedFuture("newValue") | ||
.thenAccept(LOGGER::info) | ||
.thenRun(() -> LOGGER.info("串行任务执行完成")); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/xyz/xuminghai/concurrent/ConcurrentHashMapDemo01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package xyz.xuminghai.concurrent; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* 2023/8/2 10:34 星期三<br/> | ||
* 支持完全并发检索和高预期更新并发的哈希表。此类遵循与Hashtable相同的功能规范, | ||
* 并包含与Hashtable的每个方法相对应的方法版本。然而,即使所有操作都是线程安全的, | ||
* 检索操作也不需要锁定,并且不支持以阻止所有访问的方式锁定整个表。在依赖其线程安全性但不依赖其同步细节的程序中, | ||
* 此类可与Hashtable完全互操作。 | ||
* | ||
* @author xuMingHai | ||
*/ | ||
public class ConcurrentHashMapDemo01 { | ||
|
||
/** | ||
* 日志记录器 | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentHashMapDemo01.class); | ||
|
||
private static final ConcurrentHashMap<String, String> CONCURRENT_HASH_MAP = new ConcurrentHashMap<>(); | ||
|
||
|
||
public static void main(String[] args) { | ||
// 填充初始数据 | ||
CONCURRENT_HASH_MAP.put("key1", "value1"); | ||
CONCURRENT_HASH_MAP.put("key2", "value2"); | ||
CONCURRENT_HASH_MAP.put("key3", "value3"); | ||
|
||
new Thread(() -> { | ||
// 多线程修改同一个key | ||
CONCURRENT_HASH_MAP.replace("key1", "thread0"); | ||
// parallelismThreshold – 并行执行此操作所需的(估计)元素数量 | ||
// 串行执行 | ||
CONCURRENT_HASH_MAP.forEachEntry(Long.MAX_VALUE, entry -> LOGGER.info(entry.toString())); | ||
}).start(); | ||
|
||
new Thread(() -> { | ||
// 多线程修改同一个key | ||
CONCURRENT_HASH_MAP.replace("key1", "thread1"); | ||
// 并行执行 | ||
CONCURRENT_HASH_MAP.forEachEntry(Long.MIN_VALUE, entry -> LOGGER.info(entry.toString())); | ||
}).start(); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/xyz/xuminghai/concurrent/ConcurrentLinkedDequeDemo01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package xyz.xuminghai.concurrent; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
|
||
/** | ||
* 2023/5/17 13:19 星期三<br/> | ||
* <h1>ConcurrentLinkedDeque示例01</h1> | ||
* 基于链接节点的无界并发双端队列。 | ||
* 并发插入、删除和访问操作跨多个线程安全执行。 | ||
* 当许多线程将共享对公共集合的访问时, ConcurrentLinkedDeque是一个合适的选择。 | ||
* 与大多数其他并发集合实现一样,此类不允许使用null元素。 | ||
* 迭代器和拆分器是弱一致的 | ||
* | ||
* @author xuMingHai | ||
*/ | ||
public class ConcurrentLinkedDequeDemo01 { | ||
|
||
/** | ||
* 日志记录器 | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentLinkedDequeDemo01.class); | ||
|
||
/** | ||
* 基于链表的无界非阻塞并发双端队列 | ||
*/ | ||
private static final ConcurrentLinkedDeque<String> CONCURRENT_LINKED_DEQUE = new ConcurrentLinkedDeque<>(); | ||
|
||
public static void main(String[] args) { | ||
|
||
CONCURRENT_LINKED_DEQUE.add("hello"); | ||
|
||
new Thread(() -> { | ||
CONCURRENT_LINKED_DEQUE.addLast("world"); | ||
CONCURRENT_LINKED_DEQUE.addFirst("Hi"); | ||
}).start(); | ||
|
||
new Thread(() -> { | ||
String firstItem = CONCURRENT_LINKED_DEQUE.pollFirst(); | ||
String lastItem = CONCURRENT_LINKED_DEQUE.pollLast(); | ||
LOGGER.info("firstItem = {}", firstItem); | ||
LOGGER.info("lastItem = {}", lastItem); | ||
}).start(); | ||
|
||
// 弱一致性 | ||
for (String s : CONCURRENT_LINKED_DEQUE) { | ||
LOGGER.info("s = {}", s); | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/xyz/xuminghai/concurrent/ConcurrentLinkedQueueDemo01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package xyz.xuminghai.concurrent; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
/** | ||
* 2023/5/17 13:05 星期三<br/> | ||
* <h1>ConcurrentLinkedQueue示例01</h1> | ||
* 基于链接节点的无界线程安全队列。该队列对元素进行 FIFO(先进先出)排序。 | ||
* 队列的头部是队列中时间最长的元素。队列的尾部是在队列中时间最短的元素。 | ||
* 新元素插入队列尾部,队列检索操作获取队列头部元素。 | ||
* 当许多线程将共享对公共集合的访问时, ConcurrentLinkedQueue是一个合适的选择。 | ||
* 与大多数其他并发集合实现一样,此类不允许使用null元素。 | ||
* Concurrent集合不提供阻塞功能 | ||
* | ||
* @author xuMingHai | ||
*/ | ||
public class ConcurrentLinkedQueueDemo01 { | ||
|
||
/** | ||
* 日志记录器 | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentLinkedQueueDemo01.class); | ||
|
||
/** | ||
* 一个无界的非阻塞并发队列 | ||
*/ | ||
private static final ConcurrentLinkedQueue<String> CONCURRENT_LINKED_QUEUE = new ConcurrentLinkedQueue<>(); | ||
|
||
public static void main(String[] args) { | ||
|
||
// 多线程并发修改 | ||
new Thread(() -> { | ||
String s = CONCURRENT_LINKED_QUEUE.poll(); | ||
LOGGER.info("s = {}", s); | ||
}).start(); | ||
|
||
new Thread(() -> { | ||
String s = CONCURRENT_LINKED_QUEUE.poll(); | ||
LOGGER.info("s = {}", s); | ||
}).start(); | ||
|
||
CONCURRENT_LINKED_QUEUE.add("hello"); | ||
|
||
// 弱一致性的迭代器 | ||
for (String s : CONCURRENT_LINKED_QUEUE) { | ||
LOGGER.info("s = {}", s); | ||
} | ||
|
||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/xyz/xuminghai/concurrent/ConcurrentSkipListMapDemo01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package xyz.xuminghai.concurrent; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ConcurrentSkipListMap; | ||
|
||
/** | ||
* 2023/10/1 21:44 星期日<br/> | ||
* 可扩展的并发ConcurrentNavigableMap实现。映射根据其键的自然顺序进行排序, | ||
* 或者通过映射创建时提供的Comparator进行排序,具体取决于使用的构造函数。 | ||
* 此类实现了SkipLists 的并发变体,为containsKey 、 get 、 | ||
* put和remove操作及其变体提供预期的平均log(n)时间成本。插入、删除、更新和访问操作由多个线程安全地并发执行。 | ||
* 映射根据其键的自然顺序进行排序 | ||
* | ||
* @author xuMingHai | ||
*/ | ||
public class ConcurrentSkipListMapDemo01 { | ||
|
||
/** | ||
* 日志记录器 | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentSkipListMapDemo01.class); | ||
|
||
/** | ||
* 根据键的自然顺序进行排序。 | ||
*/ | ||
private static final ConcurrentSkipListMap<Integer, String> CONCURRENT_SKIP_LIST_MAP = new ConcurrentSkipListMap<>(); | ||
|
||
public static void main(String[] args) { | ||
CONCURRENT_SKIP_LIST_MAP.put(1, "张三"); | ||
CONCURRENT_SKIP_LIST_MAP.put(3, "王五"); | ||
CONCURRENT_SKIP_LIST_MAP.put(4, "赵六"); | ||
|
||
// 并行写入 | ||
new Thread(() -> { | ||
CONCURRENT_SKIP_LIST_MAP.put(2, "李四"); | ||
CONCURRENT_SKIP_LIST_MAP.entrySet().forEach(entry -> LOGGER.info(entry.toString())); | ||
}).start(); | ||
|
||
// 弱一致性遍历 | ||
CONCURRENT_SKIP_LIST_MAP.entrySet().forEach(entry -> LOGGER.info(entry.toString())); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/xyz/xuminghai/concurrent/ConcurrentSkipListSetDemo01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package xyz.xuminghai.concurrent; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ConcurrentSkipListSet; | ||
|
||
/** | ||
* 2023/10/1 22:03 星期日<br/> | ||
* 基于ConcurrentSkipListMap的可扩展并发NavigableSet实现。集合的元素根据其自然顺序或通过在集合创建时提供的Comparator进行排序,具体取决于使用的构造函数。 | ||
* 此实现为contains 、 add和remove操作及其变体提供了预期的平均log(n)时间成本。插入、删除和访问操作由多个线程同时安全地执行。 | ||
* 迭代器和分裂器是弱一致的。 | ||
* | ||
* @author xuMingHai | ||
*/ | ||
public class ConcurrentSkipListSetDemo01 { | ||
|
||
/** | ||
* 日志记录器 | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentSkipListSetDemo01.class); | ||
|
||
/** | ||
* 根据元素的自然顺序进行排序。 | ||
*/ | ||
private static final ConcurrentSkipListSet<Integer> CONCURRENT_SKIP_LIST_SET = new ConcurrentSkipListSet<>(); | ||
|
||
public static void main(String[] args) { | ||
CONCURRENT_SKIP_LIST_SET.add(1); | ||
CONCURRENT_SKIP_LIST_SET.add(3); | ||
CONCURRENT_SKIP_LIST_SET.add(4); | ||
|
||
// 并行写入 | ||
new Thread(() -> { | ||
CONCURRENT_SKIP_LIST_SET.add(2); | ||
CONCURRENT_SKIP_LIST_SET.forEach(item -> LOGGER.info(item.toString())); | ||
}).start(); | ||
|
||
// 弱一致性遍历 | ||
CONCURRENT_SKIP_LIST_SET.forEach(item -> LOGGER.info(item.toString())); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.