Collision of static variables for two applications in the same jvm

I have an applet application that uses several static objects (and we cannot get rid of them). The application is launched from the html page. The browser creates a single jvm for any number of tabs, and thus, if you open two tabs with this application, static variables will be shared by both of them. After that, both will work incorrectly. We tried using separate_jvm , but it does not work in every browser.

Is there any other solution?

+4
source share
1 answer

This test case demonstrates how a static field in one class can have different values ​​in the same JVM when loading a class from two instances of the class loader:

@Test public void test() throws Exception { MyLoader customLoader1 = new MyLoader(); MyLoader customLoader2 = new MyLoader(); Class<?> c1 = customLoader1.loadClass(SPECIAL_CLASS_NAME); Class<?> c2 = customLoader2.loadClass(SPECIAL_CLASS_NAME); LoadedClass o1 = (LoadedClass) c1.newInstance(); LoadedClass o2 = (LoadedClass) c2.newInstance(); o1.setStaticPart(100d); o2.setStaticPart(1d); assertEquals(100d, o1.getStaticPart()); assertEquals(1d, o2.getStaticPart()); } 

How to use a custom classloader in an applet, which I leave as an exercise for the reader. :-)

+2
source

Source: https://habr.com/ru/post/1491919/


All Articles