Skip to content

Commit a43259a

Browse files
committed
Optimize synchronzied code
1 parent 7470529 commit a43259a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

library/src/main/java/com/githang/dinny/Dinny.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
* @since 2018-11-27
1212
*/
1313
public class Dinny {
14-
private Dinny() {
15-
}
1614

1715
private static final Map<Method, ProtocolMethod> methodCache = new LinkedHashMap<>();
1816

17+
private Dinny() {
18+
}
19+
1920
@SuppressWarnings("unchecked")
2021
public static <T> T create(Class<T> protocol) {
2122
Utils.validateProtocolInterface(protocol);
@@ -32,13 +33,17 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
3233
}
3334

3435
private static ProtocolMethod loadProtocolMethod(Method method) {
36+
ProtocolMethod result = methodCache.get(method);
37+
if (result != null) {
38+
return result;
39+
}
3540
synchronized (methodCache) {
36-
ProtocolMethod result = methodCache.get(method);
41+
result = methodCache.get(method);
3742
if (result == null) {
3843
result = new ProtocolMethod.Builder(method).build();
3944
methodCache.put(method, result);
4045
}
41-
return result;
4246
}
47+
return result;
4348
}
4449
}

0 commit comments

Comments
 (0)