Class Simpledatefonnat is primarily responsible for converting and formatting dates, but in a multithreaded environment, using this class is prone to inaccurate data conversion and processing because the SimpleDateFormat class is not thread-safe.
1. Exception This example implements the use of class SimpleDateFormat to process dates in a multithreaded environment but the result is an error, which is also a problem that is easy to encounter in the development of multithreaded environments.
public class MyThread extends Thread {private SimpleDateFormat SDF; Private String datestring; Public MyThread (SimpleDateFormat SDF, String datestring) {super (); THIS.SDF = SDF; this.datestring = datestring; @Override public void Run () {try {Date dateref = Sdf.parse (datestring); String newdatestring = Sdf.format (dateref). toString (); if (!newdatestring.equals (datestring)) {System.out.println ("threadname=" + this.getname () + "error, date string:" +datestring + ", converted to the date:" +newdatestring); }} catch (ParseException e) {e.printstacktrace (); } }}
public Class Run {public static void main (string[] args) throws Interruptedexception {SimpleDateFormat SDF = new Simp Ledateformat ("Yyyy-mm-dd"); string[] Datestringarray = new string[]{"2001-01-01", "2001-01-02", "2001-01-03", "2001-01-04", "2001-01-05", " 2001-01-06 "," 2001-01-07 "," 2001-01-08 "}; mythread[] Threadarray = new MYTHREAD[10]; for (int i=0; i<8; i++) {Threadarray[i] = new MyThread (SDF, Datestringarray[i]); } for (int i=0; i<8; i++) {Threadarray[i].start (); } }}
Threadname=thread-2 error, date string: 2001-01-03, converted to: 2001-01-01
Threadname=thread-3 error, date string: 2001-01-04, converted to: 2199-12-02
Threadname=thread-1 error, date string: 2001-01-02, converted to: 2199-12-02
Threadname=thread-7 error, date string: 2001-01-08, converted to: 2001-08-06
Threadname=thread-5 error, date string: 2001-01-06, converted to: 2001-08-06 |
The results of printing from the console show that using a singleton SimpleDateFormat class to process dates in a multithreaded environment is prone to date conversion errors.
2. Resolve Exception 1: Create instances of multiple classes
public class Datetools { //Converts the date datestring of the string type to the corresponding date type public static date parse (string) by Formatpattern Formatpattern, String datestring) throws ParseException { return new SimpleDateFormat (Formatpattern). Parse ( datestring); } public static string format (string formatpattern, date date) { return new SimpleDateFormat (Formatpattern). Format ( date);} }
Modify the Run method of the Mythread class to:
@Override public Void Run () { try { //date dateref = Sdf.parse (datestring); Modify to the following code Date dateref = Datetools.parse ("Yyyy-mm-dd", datestring); String newdatestring = Sdf.format (dateref). toString (); Modify The following string newdatestring = Datetools.format ("Yyyy-mm-dd ", dateref); if (!newdatestring.equals (datestring)) { System.out.println ("threadname=" + this.getname () + "error, date string:" + DateString + ", converted to the date:" +newdatestring);} } catch (ParseException e) { e.printstacktrace (); } }
The console does not output any exceptions, and the principle of resolving processing errors is to create instances of multiple classes.
3, solve the exception Method 2: Use the Threadlocal class
As described earlier, the Threadlocal class can bind a thread to a specified object. This class can also be used to troubleshoot SimpleDateFormat class handling errors in a multithreaded environment.
Datetools modified to read as follows:
public class Datetools { private static threadlocal<simpledateformat> T1 = new threadlocal< Simpledateformat> (); public static SimpleDateFormat Getsimpledateformat (String datepattern) { SimpleDateFormat SDF = null; SDF = T1.get (); if (SDF = = null) { SDF = new SimpleDateFormat (datepattern); T1.set (SDF); } return SDF; }}
The Run method in the Mythread class is modified to read as follows:
@Override public Void Run () { try { //date dateref = Sdf.parse (datestring); Modify to the following code //date dateref = Datetools.parse ("Yyyy-mm-dd", datestring); Date dateref = Datetools.getsimpledateformat ("Yyyy-mm-dd"). Parse (datestring); String newdatestring = Sdf.format (dateref). toString (); modify the following//string newdatestring = Datetools.format (" Yyyy-mm-dd ", dateref); String newdatestring = Datetools.getsimpledateformat ("Yyyy-mm-dd"). Format (dateref); if (!newdatestring.equals (datestring)) { System.out.println ("threadname=" + this.getname () + "error, date string:" +datestring + ", converted to the date:" +newdatestring);} } catch (ParseException e) { e.printstacktrace (); } }
The console does not have an error message output.
Java multithreaded programming 7--simpledateformat non-thread-safe handling