Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads getting blocked JAXB

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)
like image 867
Abhishek Gayakwad Avatar asked Aug 26 '11 11:08

Abhishek Gayakwad


People also ask

Why is a thread blocked?

A Blocked state will occur whenever a thread tries to acquire lock on object and some other thread is already holding the lock. Once other threads have left and its this thread chance, it moves to Runnable state after that it is eligible pick up work based on JVM threading mechanism and moves to run state.

Is JAXB context thread safe?

JAXBContext is thread safe and should only be created once and reused to avoid the cost of initializing the metadata multiple times. Marshaller and Unmarshaller are not thread safe, but are lightweight to create and could be created per operation.

What happens when a thread is blocked?

The running thread will block when it must wait for some event to occur (response to an IPC request, wait on a mutex, etc.). The blocked thread is removed from the running array, and the highest-priority ready thread that's at the head of its priority's queue is then allowed to run.

How do you unblock a thread in Java?

interrupt() to set an interrupt flag and unblock certain operations by triggering an InterruptedException; and applying thread. join() , which causes the main thread in App to stop executing until the child thread is terminated.


2 Answers

(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");
like image 85
bdoughan Avatar answered Nov 02 '22 06:11

bdoughan


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

like image 45
abk Avatar answered Nov 02 '22 05:11

abk