线程被阻塞JAXB

时间:2011-08-26 11:21:01

标签: multithreading thread-safety jaxb

即使每次都创建了unmarshaller的新对象,线程也会被阻塞 请帮忙

"http-80-3" daemon prio=10 tid=0x000000004fabe800 nid=0x7147 waiting for monitor entry [0x0000000042401000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:457)
    - waiting to lock <0x00000000c02cce20> (a sun.net.www.protocol.jar.URLJarFile)
    at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:475)
    at java.io.FilterInputStream.read(FilterInputStream.java:66)
    at java.io.DataInputStream.readInt(DataInputStream.java:371)
    at com.sun.xml.internal.bind.v2.bytecode.ClassTailor.tailor(ClassTailor.java:165)
    at com.sun.xml.internal.bind.v2.runtime.reflect.opt.AccessorInjector.tailor(AccessorInjector.java:108)
    at com.sun.xml.internal.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:68)
    at com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:156)
    at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:245)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:79)
    at sun.reflect.GeneratedConstructorAccessor21.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
    at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)

2 个答案:

答案 0 :(得分:9)

(将我的评论移至答案)

您每次都在创建JAXBContext吗? JAXBContext是线程安全的,应该创建一次并重用。 Unmarshaller不是线程安全的,每个线程应该创建一个新的。

  

现在我正在维护一个地图(ConcurrentHashMap   上下文)由线程安全代码包装来存储所有JAXBContexts(一个   每种类型)截至目前,它运作良好。还有其他更好的建议   ??

这取决于您的申请。您还可以在许多类上创建一个JAXBContext

JAXBContext jc = JAXBContext.newInstance(A.class, B.class, C.class, D.class);

JAXBContext jc = JAXBContext.newInstance("com.foo:org.bar");

答案 1 :(得分:1)

一个很好的解决方案,可以解决JAXB可靠的性能问题。开发多线程Web应用程序时可以创建单例,这样可以提供JAXBContext:

class JAXBContextServletHelper  extends HttpServlet {
    static final JAXBContext context = initContext();
    private static JAXBContext initContext() {
        return JAXBContext.newInstance(MyClasse1.class,MyClasse2.class);
    }
}

并使用;

JAXBContext context =   JAXBContextServletHelper. initContext();
Unmarshaller u = context.createUnmarshaller();
        u.unmarshal(...);

有关此问题的更多详细信息,请访问java.net上的jaxb教程here