Re: Exception on get method clusterj
Based on your report I took a closer look at the handling of charset encoder and decoder. I found a similar multi-thread issue with encoding as well.
There are a few ways to make the code thread-safe and I wrote a micro-benchmark to test them out.
- synchronize access to a static (singleton) decoder
- get a new decoder from charset for each request
- use the decode method in charset
- pool decoders in a thread-safe queue
Here's the code snippet:
switch(strategy) {
case SYNC:
synchronized(charsetDecoder) {
result = charsetDecoder.decode(byteBuffer);
}
break;
case NEW:
CharsetDecoder newCharsetDecoder = charset.newDecoder();
result = newCharsetDecoder.decode(byteBuffer);
break;
case METHOD:
result = charset.decode(byteBuffer);
break;
case POOL:
CharsetDecoder pooledCharsetDecoder = pool.poll();
if (pooledCharsetDecoder == null) {
pooledCharsetDecoder = charset.newDecoder();
}
result = pooledCharsetDecoder.decode(byteBuffer);
pool.add(pooledCharsetDecoder);
break;
}
Before reading on, see if you can figure out which strategy performs best.
The lowest performing strategy is synchronize.
The next worst is pooling decoders by the "application".
Looking good is getting a new decoder for each request and letting the system garbage collect it after use.
The best strategy by far is using the decode method of charset which internally creates a new decoder but can optimize it by caching decoders internally.
Subject
Views
Written By
Posted
2248
April 09, 2015 09:16AM
957
April 17, 2015 08:34AM
1161
April 17, 2015 11:40AM
1063
April 17, 2015 02:53PM
920
April 17, 2015 03:35PM
1025
April 18, 2015 05:09PM
1459
April 20, 2015 01:46AM
988
April 20, 2015 11:03AM
792
April 21, 2015 05:43AM
1108
May 18, 2015 08:25PM
Re: Exception on get method clusterj
927
May 21, 2015 10:44AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.