|
35 | 35 | import java.util.concurrent.FutureTask; |
36 | 36 | import java.util.concurrent.RunnableFuture; |
37 | 37 | import java.util.concurrent.atomic.AtomicInteger; |
| 38 | +import java.util.Collections; |
38 | 39 |
|
39 | 40 | public class Runtime { |
40 | 41 | 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) |
103 | 104 | "Primitive types need to be manually wrapped in their respective Object wrappers.\n" + |
104 | 105 | "If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument."; |
105 | 106 |
|
106 | | - private HashMap<Integer, Object> strongInstances = new HashMap<>(); |
| 107 | + private Map<Integer, Object> strongInstances = new HashMap<>(); |
107 | 108 |
|
108 | | - private HashMap<Integer, WeakReference<Object>> weakInstances = new HashMap<>(); |
| 109 | + private Map<Integer, WeakReference<Object>> weakInstances = new HashMap<>(); |
109 | 110 |
|
110 | | - private NativeScriptHashMap<Object, Integer> strongJavaObjectToID = new NativeScriptHashMap<Object, Integer>(); |
| 111 | + private Map<Object, Integer> strongJavaObjectToID = new NativeScriptHashMap<Object, Integer>(); |
111 | 112 |
|
112 | | - private NativeScriptWeakHashMap<Object, Integer> weakJavaObjectToID = new NativeScriptWeakHashMap<Object, Integer>(); |
| 113 | + private Map<Object, Integer> weakJavaObjectToID = new NativeScriptWeakHashMap<Object, Integer>(); |
113 | 114 |
|
114 | | - private final Map<Class<?>, JavaScriptImplementation> loadedJavaScriptExtends = new HashMap<Class<?>, JavaScriptImplementation>(); |
| 115 | + private Map<Class<?>, JavaScriptImplementation> loadedJavaScriptExtends = new HashMap<Class<?>, JavaScriptImplementation>(); |
115 | 116 |
|
116 | 117 | private final java.lang.Runtime dalvikRuntime = java.lang.Runtime.getRuntime(); |
117 | 118 |
|
@@ -215,6 +216,14 @@ public Runtime(StaticConfiguration config, DynamicConfiguration dynamicConfigura |
215 | 216 | if (dynamicConfiguration.mainThreadScheduler != null) { |
216 | 217 | this.mainThreadHandler = dynamicConfiguration.mainThreadScheduler.getHandler(); |
217 | 218 | } |
| 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 | + } |
218 | 227 |
|
219 | 228 | classResolver = new ClassResolver(classStorageService); |
220 | 229 | currentRuntime.set(this); |
|
0 commit comments