SimpleDateFormat在多线程情况下会出现线程不安全的情况,故用ThreadLoacl 处理
/**
* 用ThreadLocal处理simplDateFormat线程不安全
*/
public class DateUtil { public static SimpleDateFormat renderSimpleDateFormat(String pattern) {
ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(pattern);
}
};
return threadLocal.get();
} public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(10);
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
String str = DateUtil.renderSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(str);
countDownLatch.countDown();
}
}).start();
}
//TODO: await 不是 wait
countDownLatch.await();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}

ThreadLocal 解决simpledateformat线程不安全的更多相关文章

  1. 【Java并发编程】12、ThreadLocal 解决SimpleDateFormat非线程安全

    大致意思:Tim Cull碰到一个SimpleDateFormat带来的严重的性能问题,该问题主要有SimpleDateFormat引发,创建一个 SimpleDateFormat实例的开销比较昂贵, ...

  2. 解决SimpleDateFormat线程安全问题

    package com.tanlu.user.util; import java.text.DateFormat; import java.text.ParseException; import ja ...

  3. ThreadLocal解决SimpleDateFormat多线程安全问题中遇到的困惑

    测试代码: public class Main { public static void main(String[] args) { for (int k = 0; k < 10; k++) { ...

  4. SimpleDateFormat线程不安全及解决办法

    原文链接:https://blog.csdn.net/csdn_ds/article/details/72984646 以前没有注意到SimpleDateFormat线程不安全的问题,写时间工具类,一 ...

  5. SimpleDateFormat线程不安全及解决办法(转)

    以前没有注意到SimpleDateFormat线程不安全的问题,写时间工具类,一般写成静态的成员变量,不知,此种写法的危险性!在此讨论一下SimpleDateFormat线程不安全问题,以及解决方法. ...

  6. ThreadLocal解决线程安全问题

    一.线程安全问题产生的原因 线程安全问题都是由全局变量及静态变量引起的 二.线程安全问题 SimpleDateFormate sdf = new SimpleDateFormat();使用sdf.pa ...

  7. SimpleDateFormat线程不安全及解决的方法

    一. 为什么SimpleDateFormat不是线程安全的? Java源代码例如以下: /** * Date formats are not synchronized. * It is recomme ...

  8. SimpleDateFormat线程不安全(转)

    有三种方法可以解决以上安全问题.  1).使用同步 package com.bijian.study.date; import java.text.ParseException; import jav ...

  9. SimpleDateFormat线程不安全的5种解决方案!

    1.什么是线程不安全? 线程不安全也叫非线程安全,是指多线程执行中,程序的执行结果和预期的结果不符的情况就叫做线程不安全. ​ 线程不安全的代码 SimpleDateFormat 就是一个典型的线程不 ...

随机推荐

  1. How do I add a simple onClick event handler to a canvas element?

    How do I add a simple onClick event handler to a canvas element? When you draw to a canvas element, ...

  2. Zabbix通过SNMP监控HP Gen10服务器的硬件

    http://www.zmzblog.com/monitor/zabbix-how-to-monitoring-hp-gen10-server-hardware.html

  3. imu tool使用

    安装imu tool sudo apt-get install ros-melodic-imu-tools launch文件: <!-- imu_node launch file--> & ...

  4. Vue知识整理11:列表渲染(v-for来实现)

    简单的v-for结构显示迭代数据 通过value别名 显示下面各个属性值 通过index 和key获取同类数组索引,或者不同属性的key属性名

  5. Scala的to和until

    object test03 { def main(args: Array[String]): Unit = { //to 每次迭代为1 val to1= to print("to1" ...

  6. 第十届山东省acm省赛补题(2)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124 L Median Time Limit: 1 Second      ...

  7. 手写一个IOC容器

    链接:https://pan.baidu.com/s/1MhKJYamBY1ejjjhz3BKoWQ 提取码:e8on 明白什么是IOC容器: IOC(Inversion of Control,控制反 ...

  8. Oracle SQL调优

    在多数情况下,Oracle使用索引t来更快地遍历表,优化器主要根据定义的索引来提高性能. 但是,如果在SQL语句的where子句中写的SQL代码不合理,就会造成优化器删去索引而使用全表扫描,一般就这种 ...

  9. vue中的provide和inject

    vue中的provide和inject:https://blog.csdn.net/viewyu12345/article/details/83011618

  10. JAVA知识点总结(六)(集合)

    第十九章 集合 一.数组弊端: 数组长度是固定的,无法继续添加元素. 二.什么是集合: Java提供一个集合类,它的长度是可以改变的,能储存任意的对象,长度随着元素的增加而增加. 三.集合和数组的区别 ...