Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ThreadFactory, JDK21 #14

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mvn clean install

| 版本 | 说明 |
| ------ | ------------------------------------------------------------ |
| 1.0.11 | 1. 添加 ConcurrentHashSet <br />2. 添加 PlatformThreadFactory<br />3. 添加 VirtualThreadFactory<br />4. 升级为支持 JDK 21,不再支持 JDK 17 |
| 1.0.10 | 1. 添加 SimpleJSON(仅实现对象转 JSONString)<br />2. 升级为支持 JDK 17,不再支持 JDK 8 |
| 1.0.9 | 1. 添加 RandomUtils |
| 1.0.8 | 1. 添加 Maps.newHashMap方法 |
Expand Down
114 changes: 114 additions & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,115 @@ public class ArrayUtilsTest {
}
```

### 4.4. Sets

主要用于创建 Set

```java
class SetsTest {

/**
* 创建 HashSet
*/
@Test
public void newHashSet() {
HashSet<String> set = Sets.newHashSet();
Assertions.assertEquals(0, set.size());
}

/**
* 使用期望容量创建 HashSet
*/
@Test
public void testNewHashSet() {
HashSet<String> set = Sets.newHashSet(8);
Assertions.assertEquals(0, set.size());
}

/**
* 集合对象转换为 HashSet
*/
@Test
public void testNewHashSet1() {
int size = 5;
ArrayList<Integer> list = getArrayList(size);
HashSet<Integer> set = Sets.newHashSet(list);
Assertions.assertEquals(size, set.size());
}

/**
* 创建 LinkedHashSet
*/
@Test
public void newLinkedHashSet() {
LinkedHashSet<String> set = Sets.newLinkedHashSet();
Assertions.assertEquals(0, set.size());
}

/**
* 使用期望容量创建 LinkedHashSet
*/
@Test
public void testNewLinkedHashSet() {
LinkedHashSet<String> set = Sets.newLinkedHashSet(8);
Assertions.assertEquals(0, set.size());
}

/**
* 集合对象转换为 LinkedHashSet
*/
@Test
public void testNewLinkedHashSet1() {
int size = 5;
ArrayList<Integer> list = getArrayList(size);
LinkedHashSet<Integer> set = Sets.newLinkedHashSet(list);
Assertions.assertEquals(size, set.size());
}

/**
* 创建 ConcurrentHashSet
*/
@Test
void newConcurrentHashSet() {
ConcurrentHashSet<String> set = Sets.newConcurrentHashSet();
Assertions.assertEquals(0, set.size());
}

/**
* 使用期望容量创建 ConcurrentHashSet
*/
@Test
void testNewConcurrentHashSet() {
ConcurrentHashSet<String> set = Sets.newConcurrentHashSet(1);
Assertions.assertEquals(0, set.size());
}

/**
* 集合对象转换为 LinkedHashSet
*/
@Test
void testNewConcurrentHashSet1() {
int size = 5;
ArrayList<Integer> list = getArrayList(size);
ConcurrentHashSet<Integer> set = Sets.newConcurrentHashSet(list);
Assertions.assertEquals(size, set.size());
}

private static ArrayList<Integer> getArrayList(int size) {
ArrayList<Integer> list = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
list.add(i);
}
return list;
}

}
```

### 4.5. ConcurrentHashSet

类似于 HashSet 的实现,底层使用 ConcurrentHashMap 作为存储。

## 5. 安全工具类

### 5.1. DigestUtils
Expand Down Expand Up @@ -2356,3 +2465,8 @@ class SimpleJSONTest {
}
```

## 10. 线程与并发

PlatformThreadFactory:平台线程池

VirtualThreadFactory:虚拟线程池
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.igeeksky.xtool.core.collection;

import java.io.Serial;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* @author Patrick.Lau
* @since 1.0.0 2024/7/1
*/
public class ConcurrentHashSet<E> extends AbstractSet<E> {
public class ConcurrentHashSet<E> extends AbstractSet<E> implements Serializable {

private final ConcurrentMap<E, Boolean> delegate;
@Serial
private static final long serialVersionUID = 8822098000974922850L;

private static final Object PRESENT = new Object();

private final transient ConcurrentHashMap<E, Object> delegate;

public ConcurrentHashSet() {
this.delegate = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -65,19 +71,24 @@ public <T> T[] toArray(T[] a) {

@Override
public boolean add(E e) {
return this.delegate.put(e, Boolean.TRUE) == null;
return this.delegate.put(e, PRESENT) == null;
}

@Override
public boolean remove(Object o) {
return this.delegate.remove(o) != null;
}

@Override
public Spliterator<E> spliterator() {
return this.delegate.keySet().spliterator();
}

@Override
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
Iterator<Map.Entry<E, Boolean>> it = this.delegate.entrySet().iterator();
Iterator<Map.Entry<E, Object>> it = this.delegate.entrySet().iterator();
while (it.hasNext()) {
if (!c.contains(it.next().getKey())) {
it.remove();
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/igeeksky/xtool/core/collection/SetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@
*/
class SetsTest {

/**
* 创建 HashSet
*/
@Test
public void newHashSet() {
HashSet<String> set = Sets.newHashSet();
Assertions.assertEquals(0, set.size());
}

/**
* 使用期望容量创建 HashSet
*/
@Test
public void testNewHashSet() {
HashSet<String> set = Sets.newHashSet(8);
Assertions.assertEquals(0, set.size());
}

/**
* 集合对象转换为 HashSet
*/
@Test
public void testNewHashSet1() {
int size = 5;
Expand All @@ -33,18 +42,27 @@ public void testNewHashSet1() {
Assertions.assertEquals(size, set.size());
}

/**
* 创建 LinkedHashSet
*/
@Test
public void newLinkedHashSet() {
LinkedHashSet<String> set = Sets.newLinkedHashSet();
Assertions.assertEquals(0, set.size());
}

/**
* 使用期望容量创建 LinkedHashSet
*/
@Test
public void testNewLinkedHashSet() {
LinkedHashSet<String> set = Sets.newLinkedHashSet(8);
Assertions.assertEquals(0, set.size());
}

/**
* 集合对象转换为 LinkedHashSet
*/
@Test
public void testNewLinkedHashSet1() {
int size = 5;
Expand All @@ -53,18 +71,27 @@ public void testNewLinkedHashSet1() {
Assertions.assertEquals(size, set.size());
}

/**
* 创建 ConcurrentHashSet
*/
@Test
void newConcurrentHashSet() {
ConcurrentHashSet<String> set = Sets.newConcurrentHashSet();
Assertions.assertEquals(0, set.size());
}

/**
* 使用期望容量创建 ConcurrentHashSet
*/
@Test
void testNewConcurrentHashSet() {
ConcurrentHashSet<String> set = Sets.newConcurrentHashSet(1);
Assertions.assertEquals(0, set.size());
}

/**
* 集合对象转换为 LinkedHashSet
*/
@Test
void testNewConcurrentHashSet1() {
int size = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class PlatformThreadFactoryTest {

private static final PlatformThreadFactory threadFactory = new PlatformThreadFactory("platform-thread-");

/**
* 创建新线程
*/
@Test
void newThread() {
for (int i = 0; i < 10; i++) {
Expand Down
Loading