#977 DateAxis threading issue

1.0.x
closed-fixed
David Gilbert
General (896)
5
2013-11-23
2010-01-14
dfelsenthal
No

The createStandardDateTickUnits method is static. It uses SimpleDateFormat which uses Calendar which is not thread safe.

This causes random exceptions such as:
java.lang.ArrayIndexOutOfBoundsException: 84
at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:436)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2081)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1996)
at java.util.Calendar.setTimeInMillis(Calendar.java:1071)
at java.util.Calendar.setTime(Calendar.java:1037)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:803)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:796)
at java.text.DateFormat.format(DateFormat.java:314)
at org.jfree.chart.axis.DateTickUnit.dateToString(DateTickUnit.java:220)

Discussion

  • Scott Dudley
    Scott Dudley
    2013-11-15

    I believe the problem is not in the static createStandardDateTickUnits method (static methods are not inherently bad), but rather, in the following static initializer in DateAxis, which later gets used in the constructor:

        /** The default date tick unit. */
        public static final DateTickUnit DEFAULT_DATE_TICK_UNIT
                = new DateTickUnit(DateTickUnitType.DAY, 1, new SimpleDateFormat());
    
    [...]
    
        public DateAxis(String label, TimeZone zone, Locale locale) {
            super(label, DateAxis.createStandardDateTickUnits(zone, locale));
            setTickUnit(DateAxis.DEFAULT_DATE_TICK_UNIT, false, false);
    

    You should be able to fix this easily in your own code by calling something like this after creating the object:

     axis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1, new SimpleDateFormat()), false, false);
    
     
  • David Gilbert
    David Gilbert
    2013-11-23

    Thanks for the analysis. For the 1.0.17 release, I have deprecated the DEFAULT_DATE_TICK_UNIT constant and replaced its use in the code with a newly created instance.

     
  • David Gilbert
    David Gilbert
    2013-11-23

    • status: open --> closed-fixed
    • assigned_to: David Gilbert
    • Group: --> 1.0.x