2 Replies Latest reply on Jan 12, 2007 12:29 PM by 843790

    Thread blocking on java.nio.charset.CoderResult

      Hello all,

      I have a multi-threaded app which does some fairly intestive string operations (basically extracts text from documents for indexing a search system).

      I am seeing a massive bottleneck around the java.nio.charset.CoderResult class. When profiling, I see a whole stack of threads blocking on (waiting for) a monitor on this java.nio.charset.CoderResult class. Seems to be a result of string encoding/decoding (I am often encoding strings as UTF-8).

      Anyone know why the JVM would want my threads to sync on this class? It's creating a huge performance issue for my app. Approximately 15% of ALL the processing time is spent waiting for this class.

        • 1. Re: Thread blocking on java.nio.charset.CoderResult
          I would guess that you're using some of the static methods in the CoderResult class. The static methods CoderResult.unmappableCache(), CoderResult.malformedForLength() and CoderResult.malformedCache all use a static inner class called Cache. Its get() method is synchronized on Cache.class. Since the Cache inner-class is static, any part of your multi-threaded application that goes through the Cache.get() method is going to be waiting for the lock on Cache.class.

          Could you create a CoderResult instance for each thread? That would mean that there would be a different static Cache class for each thread, reducing the number of threads competing for the Cache.class lock.

          I'd have to see some of your code to give a better answer.

          • 2. Re: Thread blocking on java.nio.charset.CoderResult
            Hi all

            I am seeing the exact same behaviour in an application doing lots of conversion from byte arrays to UTF-8 strings. And I can confirm that it is in Cache.get that the blocking occurs.

            The code I am using is this (assume that the charset is defined somewhere else):
                        final CharBuffer buffer = charset.decode(ByteBuffer.wrap(out.toByteArray()));
                        return buffer.toString();
            What do you mean by "Could you create a CoderResult instance for each thread?"?

            Best regards,