fix dead lock bug

This commit is contained in:
Looly
2020-05-19 18:31:30 +08:00
parent 008855fafb
commit f7bf950073
9 changed files with 125 additions and 29 deletions

View File

@@ -3,6 +3,7 @@ package cn.hutool.core.lang;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.thread.ThreadUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
public class SingletonTest {
@@ -27,4 +28,24 @@ public class SingletonTest {
private String name;
private String age;
}
/**
* 测试单例构建属性锁死问题
* C构建单例时候同时构建B此时在SimpleCache中会有写锁竞争写入C时获取了写锁此时要写入B也要获取写锁
*/
@Test(timeout = 1000L)
public void reentrantTest(){
final C c = Singleton.get(C.class);
Assert.assertEquals("aaa", c.getB().getA());
}
@Data
static class B{
private String a = "aaa";
}
@Data
static class C{
private B b = Singleton.get(B.class);
}
}