Other |
---|
1.4.0 betaFixed |
Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
Name: boT120536 Date: 01/22/2001 java version "1.2.2" Classic VM (build JDK-1.2.2-W, native threads, symcjit) I have a static instance of SimpleDateFormat, and I have 2 threads, each using private clones of this instance. One thread uses the parse() method to convert a String to a Date, the other uses the format method to convert a Date to a String. Even though each thread has a clone of the SimpleDateFormat instance, the results of the parse and/or format are corrupted after a few seconds due to apparent concurrent conflicts. The following code demonstrates the problem: import java.io.*; import java.text.*; import java.util.*; public class SDFClone { static boolean stopped_ = false; static final String TIME_STRING = "2000/11/18 00:01:00"; static final String TIME_STRING_FULL = "11/18/2000 00:01:00 PST"; static final long UTC_LONG = 974534460000L; static SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); public SDFClone(String[] args) { stopped_ = false; DateParseThread d1 = new DateParseThread(); DateFormatThread d2 = new DateFormatThread(); d1.start(); d2.start(); try { Thread.sleep(100000); } catch (Exception e) {} stopped_ = true; } class DateParseThread extends Thread { public void run () { SimpleDateFormat sdf = (SimpleDateFormat)SDFClone.sdf_.clone(); try { int i = 0; while (! SDFClone.stopped_) { Date date =sdf.parse(SDFClone.TIME_STRING); long t = date.getTime(); i++; if (t != SDFClone.UTC_LONG ) { throw new ParseException("Error: " + i + " (" +sdf.format(date) + ") " + t + " != " + SDFClone.UTC_LONG, 0); } } } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } class DateFormatThread extends Thread { public void run () { SimpleDateFormat sdf = (SimpleDateFormat)SDFClone.sdf_.clone(); int i = 0; while (! SDFClone.stopped_) { i++; String s = sdf.format(new Date(974534460000L)); if (!s.equals(SDFClone.TIME_STRING)) { System.err.println("Date Error: " + i + " " + s + " != " +SDFClone.TIME_STRING_FULL); System.exit(1); } } } } public static void main (String[] args) { new SDFClone(args); } } This defect caused corruption in parsed input dates in an accounting product. The result was that incorrect resource usage was attributed to users - very bad for an accounting product!!! (Review ID: 115583) ======================================================================
|