JDK-6898310 : (cs) Charset cache lookups should be synchronized
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2009-11-05
  • Updated: 2013-04-16
  • Resolved: 2011-06-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
Other JDK 6 JDK 7 JDK 8
1.4.2_33 b02Fixed 6u30Fixed 7u2Fixed 8Fixed
Related Reports
Relates :  
Relates :  
Description
Charset cache access method, lookup() needs to be synchronized.
One lookup() had been already done as the fix for 4685305.

Considering to the purpose of the above resolution, 
one more lookup() should be synchronized as well.
Specifically speaking, the line#182 should be synchronized.

=== ./jdk/share/classes/sun/nio/cs/AbstractCharsetProvider.java (jdk7b75)===
....
�� 158      public final Charset charsetForName(String charsetName) {
�� 159          synchronized (this) {              <===== This is the fix for 4685305
�� 160              init();
�� 161              return lookup(canonicalize(charsetName));
�� 162          }
�� 163      }
�� 164
   165      public final Iterator<Charset> charsets() {
   166
   167          final ArrayList ks;
   168          synchronized (this) {
   169              init();
   170              ks = new ArrayList(classMap.keySet());
   171          }
   172
   173          return new Iterator<Charset>() {
   174                  Iterator i = ks.iterator();
   175
   176                  public boolean hasNext() {
   177                      return i.hasNext();
   178                  }
   179
   180                  public Charset next() {
   181                      String csn = (String)i.next();
   182                      return lookup(csn);      <====== Should this line be synchronized ?
   183                  }
   184
   185                  public void remove() {
   186                      throw new UnsupportedOperationException();
   187                  }
   188              };
   189      }
....
======================

Comments
EVALUATION we can add the extra synchronised block as requested or perhaps even just synchronise the lookup method call.
2011-04-11