Skip to content

Commit

Permalink
[fix], 使用线程安全的list (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
IiiinSea authored Mar 4, 2024
2 parents b422e6c + 126295c commit 7d795da
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
17 changes: 7 additions & 10 deletions jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -53,12 +53,14 @@ public static String mock(String template) {
}

public static List<String> batchMock(String template, int number) {
return batchOperation(new ArrayList<>(), () -> mock(template), number);
return batchOperation(() -> mock(template), number);
}

public static List<String> batchOperation(List<String> res, Supplier<String> supplier, int number) {
public static List<String> batchOperation(Supplier<String> supplier, int number) {
List<String> res = new CopyOnWriteArrayList<>();

int batchNumber = 1000;
int n = ((number - res.size()) / batchNumber) + 1;
int n = (number / batchNumber) + 1;

for (int j = 0; j <= n; j++) {
int defaultNumber = batchNumber;
Expand All @@ -71,12 +73,7 @@ public static List<String> batchOperation(List<String> res, Supplier<String> sup
});
}

if (res.size() >= number) {
List<String> res1 = res.subList(0, number);
return res1;
} else {
return batchOperation(res, supplier, number);
}
return res.subList(0, number);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void mockjsTestRandom() {
public void mockjsTestRandomBatch() {
String input = "Mock.Random.title(3, 5)";
long begin = System.currentTimeMillis();
List<String> output = MockJsUtils.batchMock(input, 9999);
List<String> output = MockJsUtils.batchMock(input, 99999);
long costtime = System.currentTimeMillis() - begin;
System.out.println(output);
}
Expand Down
27 changes: 27 additions & 0 deletions jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static run.mone.sysFunc.SysFuncConst.*;

Expand Down Expand Up @@ -48,6 +51,30 @@ public static String gen(String funcDesc) {
}
}

public static List<String> batchGen(String funcDesc, int number) {
return batchOperation(() -> gen(funcDesc), number);
}

public static List<String> batchOperation(Supplier<String> supplier, int number) {
List<String> res = new CopyOnWriteArrayList<>();

int batchNumber = 1000;
int n = (number / batchNumber) + 1;

for (int j = 0; j <= n; j++) {
int defaultNumber = batchNumber;
IntStream.range(0, defaultNumber)
.parallel() // 将流转换为并行流
.forEach(i -> {
// 这里是要并行执行的操作
String mockData = supplier.get();
res.add(mockData);
});
}

return res.subList(0, number);
}

private static String subString(List<String> funcParamList, String defaultStr) {

//substring(int beginIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.junit.Test;
import run.mone.sysFunc.SysFuncUtils;

import java.util.List;

@Ignore
public class SysFuncTest {

Expand All @@ -25,6 +27,12 @@ public void testRandomNumber() {
System.out.println(res);
}

@Test
public void testRandomNumberBatch() {
List<String> res = SysFuncUtils.batchGen("${java.randomNumber(2,11)}", 6);
System.out.println(res);
}



}

0 comments on commit 7d795da

Please sign in to comment.