SimpleDateFormat非线程安全

 

SimpleDateFormat非线程安全

 

package tdh.common;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

  public static void main(String[] args) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date today = new Date();
    Date tomorrow = new Date(today.getTime() + 1000 * 60 * 60 * 24);
    System.out.println(today); // 今天是2011-10-24
    System.out.println(tomorrow); // 明天是2011-10-25
    Thread thread1 = new Thread(new Thread1(dateFormat, today));
    thread1.start();
    Thread thread2 = new Thread(new Thread2(dateFormat, tomorrow));
    thread2.start();
  }
  
}

class Thread1 implements Runnable {
  private SimpleDateFormat dateFormat;
  private Date date;

  public Thread1(SimpleDateFormat dateFormat, Date date) {
    this.dateFormat = dateFormat;
    this.date = date;
  }

  public void run() {
    for (;;) {// 一直循环到出问题为止吧。
      String strDate = dateFormat.format(date);
      // 如果不等于2011-10-24,证明出现线程安全问题了!!!!
      if (!"2011-10-24".equals(strDate)) {
        System.err.println("today=" + strDate);
        System.exit(0);
      }
    }
  }
}

class Thread2 implements Runnable {
  private SimpleDateFormat dateFormat;
  private Date date;

  public Thread2(SimpleDateFormat dateFormat, Date date) {
    this.dateFormat = dateFormat;
    this.date = date;
  }

  public void run() {
    for (;;) {
      String strDate = dateFormat.format(date);
      // 如果不等于2011-10-25,证明出现线程安全问题了!!!!
      if (!"2011-10-25".equals(strDate)) {
        System.err.println("tomorrow=" + strDate);
        System.exit(0);
      }
    }
  }
}

 

 

转载 
http://www.cnblogs.com/hyddd/articles/1643978.html

 

 

你可能感兴趣的:(java)