用SimpleDateFormat把字符串转换成Date的时候报NumberFormatException

今天用for循环产生多条线程去缓存里面取数据,数据就是字符串形式的日期,我如果用for循环连续的产生线程去取的话,就会报NumberFormatException,有些时候说 For input string: "",有些时候说 multiple points ,还有些会更怪,类似于For input string: "112011.E112011E44" 这样的log,但当我如果用for循环产生一个线程后,然后睡1毫秒再产生下一个线程的话,又不会产生这样的错误,其实那些缓存着的数据本身格式是没有问题的,因为我打印出来过,而且从我第二种产生for循环的方式也可以看出是没有问题的。不知道是怎么回事

[code="java"]
public class MultiThreadCalendarTest {
static ICalendarService cal = ReferenceDataServiceFactory
.getCalendarService();

public static void main(String[] args) throws Exception {

    /**
     * Here is used to put the data into the cache.
     */
    cal.getNonBusinessDates("DW02", "2011");
    /**
     * MultiThreads get the data from the cache.
     */
    for (int i = 0; i < 2; i++) {
        new Thread() {
            public void run() {
                try {
                    List<CountryDate> list2 = cal.getNonBusinessDates(
                            "DW02", "2011");
                    for (CountryDate date : list2) {
                        System.out.println(Thread.currentThread().getName()
                                + "  " + date);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            };
        }.start();
        // Thread.sleep(1);
    }
}

}
[/code]

注意其中的这段:
[code="java"]
Date formats are not synchronized.

  • It is recommended to create separate format instances for each thread.
  • If multiple threads access a format concurrently, it must be synchronized [/code]

你好,出现这样的原因是因为: SimpleDateFormat 这个类不是线程安全的

请参见:
http://rainbow702.iteye.com/admin/blogs/1155362

希望你能够采纳,谢谢。

在你的getNonBusinessDates方法上加上synchronized关键字 用来同步