Skip to content

Commit 86d1fce

Browse files
committed
[ohscript] 优化初始化id锁粒度
1 parent 4c6b221 commit 86d1fce

File tree

1 file changed

+8
-3
lines changed
  • framework/ohscript/src/main/java/modelengine/fit/ohscript/util

1 file changed

+8
-3
lines changed

framework/ohscript/src/main/java/modelengine/fit/ohscript/util/Tool.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import modelengine.fit.ohscript.script.parser.nodes.FunctionDeclareNode;
1717
import modelengine.fit.ohscript.script.semanticanalyzer.type.expressions.TypeExprFactory;
1818
import modelengine.fitframework.beans.ObjectInstantiator;
19+
import modelengine.fitframework.util.LockUtils;
1920
import modelengine.fitframework.util.ObjectUtils;
2021

2122
import net.bytebuddy.ByteBuddy;
@@ -38,6 +39,8 @@
3839
public class Tool {
3940
private static AtomicLong id = new AtomicLong(1);
4041

42+
private static final Object lock = LockUtils.newSynchronizedLock();
43+
4144
/**
4245
* 打印警告信息
4346
*
@@ -73,9 +76,11 @@ protected static void setId(long value) {
7376
/**
7477
* 初始化id,只有在id为非正数时生效
7578
*/
76-
private static synchronized void initId() {
77-
if (id.get() <= 0) {
78-
setId(1);
79+
private static void initId() {
80+
synchronized (lock) {
81+
if (id.get() <= 0) {
82+
setId(1);
83+
}
7984
}
8085
}
8186

0 commit comments

Comments
 (0)