shenjian1114 2011-08-23 15:51
浏览 396
已采纳

用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]

  • 写回答

3条回答

  • rainbow702 2011-08-23 15:58
    关注

    注意其中的这段:
    [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]
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏