Skip to content

Commit d6ca8b5

Browse files
committed
fix: multithreadedJS should use concurrent java maps
1 parent e924542 commit d6ca8b5

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

test-app/runtime/src/main/java/com/tns/Runtime.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.concurrent.FutureTask;
3636
import java.util.concurrent.RunnableFuture;
3737
import java.util.concurrent.atomic.AtomicInteger;
38+
import java.util.Collections;
3839

3940
public class Runtime {
4041
private native void initNativeScript(int runtimeId, String filesPath, String nativeLibDir, boolean verboseLoggingEnabled, boolean isDebuggable, String packageName,
@@ -103,15 +104,15 @@ public static void passSuppressedExceptionToJs(Throwable ex, String methodName)
103104
"Primitive types need to be manually wrapped in their respective Object wrappers.\n" +
104105
"If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument.";
105106

106-
private HashMap<Integer, Object> strongInstances = new HashMap<>();
107+
private Map<Integer, Object> strongInstances = new HashMap<>();
107108

108-
private HashMap<Integer, WeakReference<Object>> weakInstances = new HashMap<>();
109+
private Map<Integer, WeakReference<Object>> weakInstances = new HashMap<>();
109110

110-
private NativeScriptHashMap<Object, Integer> strongJavaObjectToID = new NativeScriptHashMap<Object, Integer>();
111+
private Map<Object, Integer> strongJavaObjectToID = new NativeScriptHashMap<Object, Integer>();
111112

112-
private NativeScriptWeakHashMap<Object, Integer> weakJavaObjectToID = new NativeScriptWeakHashMap<Object, Integer>();
113+
private Map<Object, Integer> weakJavaObjectToID = new NativeScriptWeakHashMap<Object, Integer>();
113114

114-
private final Map<Class<?>, JavaScriptImplementation> loadedJavaScriptExtends = new HashMap<Class<?>, JavaScriptImplementation>();
115+
private Map<Class<?>, JavaScriptImplementation> loadedJavaScriptExtends = new HashMap<Class<?>, JavaScriptImplementation>();
115116

116117
private final java.lang.Runtime dalvikRuntime = java.lang.Runtime.getRuntime();
117118

@@ -215,6 +216,14 @@ public Runtime(StaticConfiguration config, DynamicConfiguration dynamicConfigura
215216
if (dynamicConfiguration.mainThreadScheduler != null) {
216217
this.mainThreadHandler = dynamicConfiguration.mainThreadScheduler.getHandler();
217218
}
219+
// if multithreadedJS, make all maps concurrent or synchronized:
220+
if (config.appConfig.getEnableMultithreadedJavascript()) {
221+
this.strongInstances = new ConcurrentHashMap<>();
222+
this.weakInstances = new ConcurrentHashMap<>();
223+
this.loadedJavaScriptExtends = new ConcurrentHashMap<Class<?>, JavaScriptImplementation>();
224+
this.strongJavaObjectToID = Collections.synchronizedMap(new NativeScriptHashMap<Object, Integer>());
225+
this.weakJavaObjectToID = Collections.synchronizedMap(new NativeScriptWeakHashMap<Object, Integer>());
226+
}
218227

219228
classResolver = new ClassResolver(classStorageService);
220229
currentRuntime.set(this);

0 commit comments

Comments
 (0)