4

I have an applet application which uses several static objects (and we can not get rid of them). The application is launched from html page. Browser creates single jvm for any amount of tabs and thus if you open two tabs with this application, the static variables will be shared with both of them. Both won't work correctly after this. We've tried to use separate_jvm but it doesn't work in every browser.

Is there any other solution?

4

1 回答 1

3

这个测试用例演示了当从类加载器的两个实例加载类时,单个类中的静态字段如何在同一个 JVM 中具有不同的值:

@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());
}

如何在小程序中使用自定义类加载器我留给读者作为练习。:-)

于 2013-07-17T16:06:36.607 回答