mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复ofTry中并发环境下线程安全问题
This commit is contained in:
@@ -187,6 +187,21 @@ public class OptTest {
|
||||
Assert.assertEquals(indexOut, indexOutSituation);
|
||||
Assert.assertEquals("hutool", npe);
|
||||
Assert.assertEquals("hutool", indexOut);
|
||||
|
||||
// 多线程下情况测试
|
||||
Stream.iterate(0, i -> ++i).limit(20000).parallel().forEach(i -> {
|
||||
Opt<Object> opt = Opt.ofTry(() -> {
|
||||
if (i % 2 == 0) {
|
||||
throw new IllegalStateException(i + "");
|
||||
} else {
|
||||
throw new NullPointerException(i + "");
|
||||
}
|
||||
});
|
||||
Assert.assertTrue(
|
||||
(i % 2 == 0 && opt.getException() instanceof IllegalStateException) ||
|
||||
(i % 2 != 0 && opt.getException() instanceof NullPointerException)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@Data
|
||||
|
Reference in New Issue
Block a user