小编带你进SimpleDateFormat-多线程问题

SimpleDateFormat-多线程问题:

SimpleDateFormat类在多线程环境下中处理日期,极易出现日期转换错误的情况

1 import java.text.ParseException;

2 import java.text.SimpleDateFormat;

3 import java.util.Date;

4

5 /**

6 * 线程类

7 */

8 public class MyThread extends Thread {

9

10 private SimpleDateFormat sdf;

11 private String dateString;

12

13 public MyThread(SimpleDateFormat sdf,String dateString) {

14 this.sdf = sdf;

15 this.dateString = dateString;

16 }

17

18 @Override

19 public void run() {

20 try {

21 Date dateRef = sdf.parse(dateString);

22 String newDateString = sdf.format(dateRef).toString();

23 if(!newDateString.equals(dateString)) {

24 System.out.println("ThreadName = " + Thread.currentThread().getName()

25 + "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);

26 }

27 } catch (ParseException e) {

28 e.printStackTrace();

29 }

30 }

31 }

1 import java.text.SimpleDateFormat;

2

3 public class Test {

4

5 /**

6 * 测试单例的SimpleDateFormat类在多线程环境下中处理日期,极易出现日期转换错误的情况

7 */

8 public static void main(String[] args) {

9 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

10 String[] dateStringArray = new String[] {

11 "2000-01-01","2000-01-02","2000-01-03",

12 "2000-01-04","2000-01-05","2000-01-06",

13 "2000-01-07","2000-01-08","2000-01-09",

14 "2000-01-10"

15 };

16

17 MyThread[] threadArray = new MyThread[10];

18 for (int i = 0; i < 10; i++) {

19 threadArray[i] = new MyThread(sdf, dateStringArray[i]);

20 }

21 for (int i = 0; i < 10; i++) {

22 threadArray[i].start();

23 }

24 }

25 }

运行之后会输出很多的错误信息!

解决多线程出现的问题-为每个线程实例一个SimpleDateFormat:

1 import java.text.ParseException;

2 import java.util.Date;

3

4 /**

5 * 线程类

6 */

7 public class MyThread extends Thread {

8

9 private String dateString;

10 public MyThread(String dateString) {

11 this.dateString = dateString;

12 }

13

14 @Override

15 public void run() {

16 try {

17 Date dateRef = DateTools.parse("yyyy-MM-dd", dateString);

18 String newDateString = DateTools.format("yyyy-MM-dd",dateRef).toString();

19 if(!newDateString.equals(dateString)) {

20 System.out.println("ThreadName = " + Thread.currentThread().getName()

21 + "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);

22 }

23 } catch (ParseException e) {

24 e.printStackTrace();

25 }

26 }

27 }

1 import java.text.ParseException;

2 import java.text.SimpleDateFormat;

3 import java.util.Date;

4

5 /**

6 * 日期格式化工具类

7 */

8 public class DateTools {

9

10 public static Date parse(String formatPattern,String dateString) throws ParseException {

11 return new SimpleDateFormat(formatPattern).parse(dateString);

12 }

13

14 public static String format(String formatPattern,Date date) throws ParseException {

15 return new SimpleDateFormat(formatPattern).format(date).toString();

16 }

17 }

1 public class Test {

2

3 /**

4 * 测试,运行程序后,控制台没有任何输出,也就是转换没有任何异常,

5 * 原理:创建了多个SimpleDateFormat实例

6 */

7 public static void main(String[] args) {

8 String[] dateStringArray = new String[] {

9 "2000-01-01","2000-01-02","2000-01-03",

10 "2000-01-04","2000-01-05","2000-01-06",

11 "2000-01-07","2000-01-08","2000-01-09",

12 "2000-01-10"

13 };

14

15 MyThread[] threadArray = new MyThread[10];

16 for (int i = 0; i < 10; i++) {

17 threadArray[i] = new MyThread(dateStringArray[i]);

18 }

19 for (int i = 0; i < 10; i++) {

20 threadArray[i].start();

21 }

22 }

23 }

解决多线程出现的问题-使用ThreadLocal:

1 import java.text.ParseException;

2 import java.util.Date;

3

4 /**

5 * 线程类

6 */

7 public class MyThread extends Thread {

8

9 private String dateString;

10 public MyThread(String dateString) {

11 this.dateString = dateString;

12 }

13

14 @Override

15 public void run() {

16 try {

17 Date dateRef = DateTools.getSimpleDateFormat("yyyy-MM-dd").parse(dateString);

18 String newDateString = DateTools.getSimpleDateFormat("yyyy-MM-dd").format(dateRef).toString();

19 if(!newDateString.equals(dateString)) {

20 System.out.println("ThreadName = " + Thread.currentThread().getName()

21 + "报错了 日期字符串:" + dateString + "转换成日期为:" + newDateString);

22 }

23 } catch (ParseException e) {

24 e.printStackTrace();

25 }

26 }

27 }

1 import java.text.SimpleDateFormat;

2

3 /**

4 * 日期格式化工具类,使用ThreadLocal解决SimpleDateFormat非线程安全问题

5 */

6 public class DateTools {

7

8 private static ThreadLocal t1 = new ThreadLocal<>();

9

10 public static SimpleDateFormat getSimpleDateFormat(String datePattern) {

11 SimpleDateFormat sdf = null;

12 sdf = t1.get();

13 if(sdf == null) {

14 sdf = new SimpleDateFormat(datePattern);

15 t1.set(sdf);

16 }

17 return sdf;

18 }

19 }

1 public class Test {

2

3 /**

4 * 测试,运行程序后,控制台没有任何输出,也就是转换没有任何异常

5 * 原理:每个线程都会有自己的ThreadLocal存储全局变量,也就是每个线程都有自己的SimpleDateFormat实例

6 */

7 public static void main(String[] args) {

8 String[] dateStringArray = new String[] {

9 "2000-01-01","2000-01-02","2000-01-03",

10 "2000-01-04","2000-01-05","2000-01-06",

11 "2000-01-07","2000-01-08","2000-01-09",

12 "2000-01-10"

13 };

14

15 MyThread[] threadArray = new MyThread[10];

16 for (int i = 0; i < 10; i++) {

17 threadArray[i] = new MyThread(dateStringArray[i]);

18 }

19 for (int i = 0; i < 10; i++) {

20 threadArray[i].start();

21 }

22 }

23 }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容

  • 想必大家对SimpleDateFormat并不陌生。SimpleDateFormat 是 Java 中一个非常常用...
    可爱傻妞是我的爱阅读 1,207评论 0 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,654评论 18 139
  • 大家好,给大家介绍一下,我家乡的好山、好水、好风景;好人、好物、好风气。 “散水崖”,两道瀑布从天而降,飘...
    錯流年阅读 514评论 0 1
  • 最近看了电影《湄公河行动》,不太明白为什么大家都一边倒地对这部电影点赞好评。就是部一元叙事线性思维的主旋律影片,细...
    绛紫辰砂阅读 693评论 0 6
  • 今天是新工作的第一天,和上司熟悉了一下,表现的合群而恰当。 了解到自己初期的工作是市场预热,伴随一家新店开张,可以...
    周胜灯阅读 128评论 0 0