线程被阻塞 JAXB

发布于 2024-12-01 16:37:29 字数 2677 浏览 1 评论 0原文

即使每次创建解组器的新对象后,线程也会被阻塞 请帮忙

"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)

even after creating new object of unmarshaller every time, threads are getting blocked
Please help

"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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2024-12-08 16:37:29

(将我的评论移至答案)

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

现在我正在维护一个映射(ConcurrentHashMap
contexts )由线程安全代码包装以存储所有 JAXBContexts(一个
每种类型)到目前为止它运行良好。还有其他更好的建议吗
??

这取决于您的应用程序。您还可以在多个类上创建一个 JAXBContext

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

或者

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

(Moving my comment to an answer)

Are you creating a JAXBContext every time? JAXBContext is thread safe and should be created once and reused. An Unmarshaller is not thread safe an a new one should be created per thread.

Now I am maintaining a map (ConcurrentHashMap
contexts ) wrapped by thread safe code to store all JAXBContexts(one
per type) as of now it is working nicely. Any other better suggestion
??

It depends on your application. You can also create one JAXBContext on many classes:

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

or

JAXBContext jc = JAXBContext.newInstance("com.foo:org.bar");
我不咬妳我踢妳 2024-12-08 16:37:29

在开发多线程 Web 应用程序时,对 JAXB Context 可靠的性能故障排除的一个好解决方案可以是创建提供 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 教程此处

a good solution to performance troublesooting reliable to JAXB Context when developping multithreaded web apps can be creation of singleton which provide JAXBContext like this :

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

and call it using ;

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

for more details on this question please visit the jaxb tutorial on java.net here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文