tag:blogger.com,1999:blog-8712770457197348465.post7168210732965435054..comments2024-03-19T01:04:48.783-07:00Comments on Javarevisited: SimpleDateFormat in Java is not Thread-Safe Use Carefullyjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-8712770457197348465.post-13060096921030444462014-03-24T13:21:51.943-07:002014-03-24T13:21:51.943-07:00ThreadLocal + SimpleDateFormat = SimpleDateFormat...ThreadLocal + SimpleDateFormat = SimpleDateFormatThreadSafe <br /><br />https://gist.github.com/pablomoretti/9748230El Santohttps://www.blogger.com/profile/08150081039350361239noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32720006385786606012014-03-19T11:30:18.321-07:002014-03-19T11:30:18.321-07:00Commons Lang 3.x now has FastDateParser and FastDa...Commons Lang 3.x now has FastDateParser and FastDateFormat. These classes are thread safe and faster than SimpleDateFormat. They also support the same format/parse pattern specifications as SimpleDateFormat.Anonymoushttps://www.blogger.com/profile/17268624466262973848noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38458223441301442502012-03-27T06:17:57.850-07:002012-03-27T06:17:57.850-07:00@Anonymous, It would be similar to how you synchro...@Anonymous, It would be similar to how you synchronize shared object for mutual exclusion. here is a psuedo code<br /><br />DateFormat localFormat; //shared<br />synchronized(localFormat){<br /> localFormat.format();<br />}Javin @ blocking method in javahttp://javarevisited.blogspot.com/2012/02/what-is-blocking-methods-in-java-and.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-52434397776134602562012-03-26T06:33:15.166-07:002012-03-26T06:33:15.166-07:00Can anyone give a working code example for "c...Can anyone give a working code example for "call to format() and parse() method should be synchronized externally"? It would be a huge help for novices like me.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82434299233144467282012-03-26T05:07:29.760-07:002012-03-26T05:07:29.760-07:00@Daniel, Thanks for your comment. you may be right...@Daniel, Thanks for your comment. you may be right because its known issue from long time. many of programmer have stopped using SimpleDateFormat due to its thread-safety issue and as you mentioned static code analyzer also has pattern for detecting static SimpleDateFormat among code. It's just unfortunate Sun doesn't address it.Javin @ HashMap vs HashSethttp://javarevisited.blogspot.com/2011/09/difference-hashmap-vs-hashset-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20895151214591004502012-03-26T03:55:02.798-07:002012-03-26T03:55:02.798-07:00This is a topic Oracle/Sun are probably never goin...This is a topic Oracle/Sun are probably never going to address. I wrote about it 5 years ago http://www.danielschneller.com/2007/04/calendar-dateformat-and-multi-threading.html and still there is no change. If you do not want to move to Joda (or can't), around the same time I contributed a detector to FindBugs that looks for static fields of type DateFormat or Calendar (and their respective Daniel Schnellerhttp://www.danielschneller.comnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14482095953346912882012-03-26T02:36:20.582-07:002012-03-26T02:36:20.582-07:00Good point @Anonymous. Thanks for bringing point o...Good point @Anonymous. Thanks for bringing point of Serialization on SimpleDateFormat.Javin @ serialization interview questionshttp://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33378039704694696612012-03-26T00:16:27.157-07:002012-03-26T00:16:27.157-07:00Note that even if you do synchronize externally to...Note that even if you do synchronize externally to use SimpleDateFormat in a multi-threaded environment, there's no way to serialize a SimpleDateFormat in a thread-safe way, because it holds a reference to a Calendar, which mutates its internal state during de-/serialization<br />(see Calendar#writeObject and Calendar#readObject, the 'zone' field is changed).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27670060579533880862012-03-24T20:18:44.663-07:002012-03-24T20:18:44.663-07:00@Recordo, Thanks for your comments. If Oracle can ...@Recordo, Thanks for your comments. If Oracle can make SimpleDateFormat better and bug free that would be best. Instead of introducing lot of new library in JDK7, they could have just address this as well.Javin @ multi-threading interview questionhttp://javarevisited.blogspot.com/2011/07/java-multi-threading-interview.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6073244192325359032012-03-24T20:15:07.037-07:002012-03-24T20:15:07.037-07:00Thanks for your comment Stephan. Indeed many peopl...Thanks for your comment Stephan. Indeed many people uses JodaTime in place of SimpleDateFormat in Java. those who stick around only because of not introducing new library.Javin @ serialization interview questionshttp://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36515678072463770542012-03-24T13:13:59.566-07:002012-03-24T13:13:59.566-07:00I've simply dropped jdk date classes in favor ...I've simply dropped jdk date classes in favor to jodatime from all our java projects enforcing this rule<br />with sonar : <br /><br />http://mestachs.wordpress.com/2012/03/17/dangerous-can-be-dating-in-java-joda-to-the-rescue/Stéphan Mestachhttps://www.blogger.com/profile/14111459876234370775noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64493141849599293262012-03-24T06:32:06.802-07:002012-03-24T06:32:06.802-07:00Nice one, didn't know about this and indeed it...Nice one, didn't know about this and indeed it can be *very* relevant. Oracle should really address this ASAP, SimpleDateFormat is widely used in multi-threaded apps...<br /><br />I guess there is no reason to use it over FastDateFormat.Ricardo Zuastihttp://ricardozuasti.comnoreply@blogger.com