• Post Reply Bookmark Topic Watch Topic
  • New Topic

DateFormat and multi-threading

 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the DateFormat class to parse a date from a String. It works fine alomost all the time but I then sometimes get
the following exception:
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.text.DigitList.getLong(Unknown Source)
at java.text.DecimalFormat.parse(Unknown Source)
at java.text.SimpleDateFormat.subParse(Unknown Source)
at java.text.SimpleDateFormat.parse(Unknown Source)
at java.text.DateFormat.parse(Unknown Source)

I read in the following posting that the DateFormat class is not thread-safe: http://forum.java.sun.com/thread.jspa?threadID=497693&messageID=2906187

In my situation I have a singleton class which is used by multiple threads at the same time. The DateFormat variable is
an instance-level variable. But the date format is not changed by any thread. It is the same for all threads. Therefore,
I am thinking that the problem described in the above forum posting isn't the actual cause of my exception??

Would appreciate other views on this because ....

I cannot reproduce the issue so it is difficult to find out exactly what is causing it. However, not sure whether or not this is just a coincidence but another problem I encountered on the same run seems to be that the
Date close = standardDateFormat.parse(accountDate);
seemed to get completely mixed up between the threads. So there could be a threading issue but I'm not sure why!!
standardDateFormat is a private instance level variable which does not change. A constant is used to set the date format.
accountDate is a local variable.

When I reran the program with the exact same data a few hours later it was inserted correctly this time. No change was made to the program in the meantime.

Many Thanks
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is very impolite to post the same question to multiple forums.
http://forum.java.sun.com/thread.jspa?threadID=5165192&tstart=0
 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it impolite?

Should I ever only post a specific question on 1 forum?

Is that a new rule on Java Ranch?!?

Just trying to increase my chances of coming across somebody who can answer my question.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the link that you referenced sure looks like it may be the same problem. Why do you think that the format has to change for it to be a problem?

Anyway, it shouldn't be hard to implement what was suggested, either (1) synchronization during the parsing, or (2) use a threadlocal set of DateFormat instances.

Henry
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by d jones:
Should I ever only post a specific question on 1 forum?

Is that a new rule on Java Ranch?!?


Not a new rule, a long-standing one. See CarefullyChooseOneForum.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by d jones:
Why is it impolite?

Should I ever only post a specific question on 1 forum?

Is that a new rule on Java Ranch?!?

Just trying to increase my chances of coming across somebody who can answer my question.


Well, as the moderator here, I don't worry about the Sun forum too much. As long as you don't cross post with other JavaRanch forums, I won't complain.

However, it is one of those things that people regard as not netiquette. So there will be those who complain, and there will be many who will *not* help you because of it. (which I don't agree with, but understand)

Henry
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake, I didn't read carefully enough and see that the "cross-post" was to a completely different bulletin board. Others may disagree, but I am with Henry on this. I think it's reasonable to post to the Sun forum and to JavaRanch, so long as it's just one forum on JavaRanch.
 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

yep, it is definitely very similar and I was just thinking of making the Dateformat variable local to the method instead of an instance level variable. But I want to make sure that this is the cause of the problem first. I want to clarify something first ....

I can understand why it would be a problem if different threads were changing the format of the DataFormat object. If one thread requests a new format then another thread could potentially change the format before the original thread had a chance to parse the date with the format it requested.

But since no thread is changing any attribute in this object and they are all sending their own local (i.e. method) variable to this object then I wouldn't have expected it to cause a problem .... hmmmm...just thinking again... unless the parameter to the parse method is being assigned to an instance level variable in the DateFormat class (I doubt this though!).

I don't really understand why 2 threads each sharing the same object (DateFormat) would get confused if neither of them modify an instance level variable of this class.

My apologies if I offended anyone by posting in two different forums. Can understand that it might be wasting multiple persons time by working on the same issue. Maybe I should have included a link in my original posting indicating that I had posted on the Sun forums as well. That way anybody working on it can check if it has been answered on either forum.

Thanks
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand why 2 threads each sharing the same object (DateFormat) would get confused if neither of them modify an instance level variable of this class.


I don't understand either -- which is the point... ... in this regard, we should assume the worst, to get our stuff to work, than to assume the best, and assume that our stuff works. Especially since the fix is just a few more DateFormat objects being created.

What we do understand, and the other forum topic mentioned this, is that there is a mention in the JavaDoc that it must be synchronized.

Henry
 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

I got a response on the Sun forum as well. Seems like a plausible explanation!?!

http://forum.java.sun.com/thread.jspa?threadID=5165192&tstart=0
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'd take that to be the meaning of the warning in the DateFormat doc.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[d jones]: I don't really understand why 2 threads each sharing the same object (DateFormat) would get confused if neither of them modify an instance level variable of this class.

But they do modify an instance variable: calendar. The parse(String) method calls parse(String, ParsePosition) which calls calendar.clear() at the beginning, then starts setting fields as they are parsed from the String. I'm not sure exactly how you get a

for input string: ""

message, but there's a lot of state change going on in that method which I don't feel like analyzing in detail. It's also possible there's some other state besides the calendar variable. Anyway, it's clear that you shouldn't try to call this method concurrently from different threads. As the API indicates.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!