zoukankan      html  css  js  c++  java
  • DateFormat的线程安全问题

    在下面例子中,System.out.println(data+ " : " + tempDateStr); 打印的结果是不一样的。
    public class DateFormatIsNotThreadSafe {
          static final SimpleDateFormat dateFormat =
                new SimpleDateFormat("dd-MM-yyyy");
          
          static String[] testData = {"01-10-1999", "14-10-2001", "31-10-2007"};
          
          public static void main(String[] args){
                for (final String data : testData){            
                            new Thread(){
                                  public void run(){
                                        try {
                                              Date date = dateFormat.parse(data);
                                              String tempDateStr = dateFormat.format(date);
                                              System.out.println(data+ " : " + tempDateStr);
                                        } catch (ParseException e) {
                                              // TODO Auto-generated catch block
                                              e.printStackTrace();
                                        }
                                  }
                            }.start();
                }
          }
    }

    在运行的100个线程中,有些线程运行的结果是正确的,有些线程的运行结果是错误的
  • 相关阅读:
    BZOJ3193: [JLOI2013]地形生成
    ARG102E:Stop. Otherwise...
    51NOD1847:奇怪的数学题
    大型大常数多项式模板(已卡常...)
    CF932G Palindrome Partition
    51nod1538:一道难题(常系数线性递推/Cayley-Hamilton定理)
    HTML——meta标签
    HTTP 格式
    Node.js——Async
    设计模式——外观模式
  • 原文地址:https://www.cnblogs.com/cando/p/2293701.html
Copyright © 2011-2022 走看看