SimpleDateFormat-多线程问题:
SimpleDateFormat类在多线程环境下中处理日期,极易出现日期转换错误的情况
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
- 线程类
*/
public class MyThread extends Thread {private SimpleDateFormat sdf;
private String dateString;public MyThread(SimpleDateFormat sdf,String dateString) {
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 = " + Thread.currentThread().getName()- "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import java.text.SimpleDateFormat;
- "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);
public class Test {
/**
* 测试单例的SimpleDateFormat类在多线程环境下中处理日期,极易出现日期转换错误的情况
*/
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String[] dateStringArray = new String[] {
"2000-01-01","2000-01-02","2000-01-03",
"2000-01-04","2000-01-05","2000-01-06",
"2000-01-07","2000-01-08","2000-01-09",
"2000-01-10"
};
MyThread[] threadArray = new MyThread[10];
for (int i = 0; i < 10; i++) {
threadArray[i] = new MyThread(sdf, dateStringArray[i]);
}
for (int i = 0; i < 10; i++) {
threadArray[i].start();
}
}
}
运行之后会输出很多的错误信息!
解决多线程出现的问题-为每个线程实例一个SimpleDateFormat:
import java.text.ParseException;
import java.util.Date;
/**
- 线程类
*/
public class MyThread extends Thread {private String dateString;
public MyThread(String dateString) {
this.dateString = dateString;
}@Override
public void run() {
try {
Date dateRef = DateTools.parse("yyyy-MM-dd", dateString);
String newDateString = DateTools.format("yyyy-MM-dd",dateRef).toString();
if(!newDateString.equals(dateString)) {
System.out.println("ThreadName = " + Thread.currentThread().getName()- "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
- "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);
/**
- 日期格式化工具类
*/
public class DateTools {public static Date parse(String formatPattern,String dateString) throws ParseException {
return new SimpleDateFormat(formatPattern).parse(dateString);
}public static String format(String formatPattern,Date date) throws ParseException {
return new SimpleDateFormat(formatPattern).format(date).toString();
}
}
public class Test {/**
- 测试,运行程序后,控制台没有任何输出,也就是转换没有任何异常,
- 原理:创建了多个SimpleDateFormat实例
*/
public static void main(String[] args) {
String[] dateStringArray = new String[] {
"2000-01-01","2000-01-02","2000-01-03",
"2000-01-04","2000-01-05","2000-01-06",
"2000-01-07","2000-01-08","2000-01-09",
"2000-01-10"
};MyThread[] threadArray = new MyThread[10];
for (int i = 0; i < 10; i++) {
threadArray[i] = new MyThread(dateStringArray[i]);
}
for (int i = 0; i < 10; i++) {
threadArray[i].start();
}
}
}
解决多线程出现的问题-使用ThreadLocal:
import java.text.ParseException;
import java.util.Date;
/**
- 线程类
*/
public class MyThread extends Thread {private String dateString;
public MyThread(String dateString) {
this.dateString = dateString;
}@Override
public void run() {
try {
Date dateRef = DateTools.getSimpleDateFormat("yyyy-MM-dd").parse(dateString);
String newDateString = DateTools.getSimpleDateFormat("yyyy-MM-dd").format(dateRef).toString();
if(!newDateString.equals(dateString)) {
System.out.println("ThreadName = " + Thread.currentThread().getName()- "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import java.text.SimpleDateFormat;
- "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);
/**
- 日期格式化工具类,使用ThreadLocal解决SimpleDateFormat非线程安全问题
*/
public class DateTools {private static ThreadLocal<SimpleDateFormat> t1 = new ThreadLocal<>();
public static SimpleDateFormat getSimpleDateFormat(String datePattern) {
SimpleDateFormat sdf = null;
sdf = t1.get();
if(sdf == null) {
sdf = new SimpleDateFormat(datePattern);
t1.set(sdf);
}
return sdf;
}
}
public class Test {/**
- 测试,运行程序后,控制台没有任何输出,也就是转换没有任何异常
- 原理:每个线程都会有自己的ThreadLocal存储全局变量,也就是每个线程都有自己的SimpleDateFormat实例
*/
public static void main(String[] args) {
String[] dateStringArray = new String[] {
"2000-01-01","2000-01-02","2000-01-03",
"2000-01-04","2000-01-05","2000-01-06",
"2000-01-07","2000-01-08","2000-01-09",
"2000-01-10"
};MyThread[] threadArray = new MyThread[10];
for (int i = 0; i < 10; i++) {
threadArray[i] = new MyThread(dateStringArray[i]);
}
for (int i = 0; i < 10; i++) {
threadArray[i].start();
}
}
}
原文地址:http://blog.51cto.com/13954634/2170816