JDK-4093418 : DateFormat is not threadsafe
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.1.4,1.1.6,1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_95,windows_nt
  • CPU: generic,x86
  • Submitted: 1997-11-17
  • Updated: 2002-10-21
  • Resolved: 2002-10-21
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description

Name: joT67522			Date: 11/17/97


The DateFormat class does not appear to be
threadsafe.  It is easy to reproduce.
Run the following code.  You will get a lot of
StringIndexOutOfBoundsExceptions.
Please advise me if there is a design problem 
with my code.  Thank you.

Mike Freemon
Sr. Systems Analyst
Information Technology
Illinois Power
Decatur IL  USA
###@###.###
voice:  217-425-4197


import java.text.*;
import java.util.*;

public class Main implements Runnable
{
    private static DateFormat dateFormat;
    private static Thread thr1;
    private static Thread thr2;
    private static Thread thr3;
    private static Thread thr4;
    
	public static void main(String args[]) 
	{
        dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM);
        TimeZone tz = TimeZone.getTimeZone("CST");
        dateFormat.setTimeZone(tz);

        thr1 = new Thread(new Main());
        thr2 = new Thread(new Main());
        thr3 = new Thread(new Main());
        thr4 = new Thread(new Main());
        
        thr1.start();
        thr2.start();
        thr3.start();
        thr4.start();
        
        try
        {
            thr1.join();
            thr2.join();
            thr3.join();
            thr4.join();
        }
        catch(Exception e)
        {}
        
	} //end of method main
    
    public void run() 
    {
        Date ddd = new Date(97,10,12,8,59,0);
        long start = ddd.getTime();
        long i = 0;
        while(true)
        {
            Date dateCurrent = new Date(start + i);
            String sss = dateFormat.format(dateCurrent);  //error here
            
            i++;
            if (i%100 == 0)  //simple status msg
                System.out.println("count is " + i + "; sss is " + sss);
        }
            
    } //end of method run
    
} //end of class Main
(Review ID: 20178)
======================================================================

Comments
EVALUATION working as designed.
11-06-2004

WORK AROUND Name: joT67522 Date: 11/17/97 ======================================================================
11-06-2004

PUBLIC COMMENTS .
10-06-2004