tag:blogger.com,1999:blog-8405107760807432973.post6692901484020964609..comments2024-01-04T09:55:32.459-08:00Comments on Java Concurrency (&c): A Note on the Thread (Un)safety of Format ClassesJeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-8405107760807432973.post-22425116862559089402016-02-19T07:30:28.092-08:002016-02-19T07:30:28.092-08:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/10749204827357568677noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-45682363065464233022011-06-02T22:19:48.368-07:002011-06-02T22:19:48.368-07:00@monn - That's not generally a great idea. Wh...@monn - That's not generally a great idea. What if someone else changed the date format slightly, so that that didn't work, but didn't notice the fact that you had used compareTo() to compare them?<br /><br />When you have a typesafe way of doing something, relying on clever tricks isn't always the best way to go.Jeremy Mansonhttps://www.blogger.com/profile/04241094734813086257noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-34999359590768275042011-06-01T07:22:11.674-07:002011-06-01T07:22:11.674-07:00For this particular case, isn't easier just to...For this particular case, isn't easier just to return str.compareTo(str2) < 0?monnhttps://www.blogger.com/profile/13979763502469183042noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-29310009020543714412010-09-12T21:55:37.099-07:002010-09-12T21:55:37.099-07:00@Anonymous - yes, that would be easier, but as you...@Anonymous - yes, that would be easier, but as you say, it is less concurrent. You really don't want to have a lock hidden away in your program that a lot of threads will be banging away on, if you can avoid it.Jeremy Mansonhttps://www.blogger.com/profile/04241094734813086257noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-64697249581390516442010-07-05T13:26:54.499-07:002010-07-05T13:26:54.499-07:00Wouldn't it be much easier just to wrap Simple...Wouldn't it be much easier just to wrap SimpleDateFormat in a threadsafe enclosure:<br /><br />new DateFormat() {<br /> private SimpleDateFormat _df =..;<br /> public synchronized String format(...) {<br /> return _df.format(..);<br /> }<br /> ..<br />}<br /><br />If the cost of building/rebuilding is too high.. then maybe the cost of synchronizing isn't.<br /><br />Of courseAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-36269524945052712152010-06-13T11:18:26.085-07:002010-06-13T11:18:26.085-07:00@IIT: If you are only using SimpleDateFormat in a ...@IIT: If you are only using SimpleDateFormat in a single thread, then constructing it in that thread is fine.<br /><br />As for initializing with a variable, I can think of several solutions, but it really depends on your code.Jeremy Mansonhttps://www.blogger.com/profile/04241094734813086257noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-16286378178263474492010-05-20T06:13:07.237-07:002010-05-20T06:13:07.237-07:00Will threadlocal code still prove to be better per...Will threadlocal code still prove to be better performing as compared to the original posted code if one thread invokes new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") only once in the lifetime. Also how to deal with threadlocal if instead of "yyyy-MM-dd HH:mm:ss.SSS" you want to pass it as a variable.Unknownhttps://www.blogger.com/profile/00933084005096615181noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-14220708279525014082010-02-08T04:55:26.257-08:002010-02-08T04:55:26.257-08:00I think the real issue is that Sun did not clearly...I think the real issue is that Sun did not clearly explain that the Format classes were based on the Factory Pattern, so ARE mutable, thus NOT thread-safe!<br /><br />I've been using ThreadLocal since I read THE Java Currency book, so get used to it!<br /><br />A Professional Java DeveloperAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-9878223293336881512010-01-27T02:10:08.595-08:002010-01-27T02:10:08.595-08:00jeremy, your example, though correct is incomlpete...jeremy, your example, though correct is incomlpete. Check out http://www.javaspecialists.eu/archive/Issue172.html . But I agree, joda-time is way simplersander hautvasthttps://www.blogger.com/profile/13533875706968075694noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-72450197869224670462010-01-11T10:40:15.952-08:002010-01-11T10:40:15.952-08:00@Sumanth - in this case, you read the JavaDoc care...@Sumanth - in this case, you read the JavaDoc carefully. Ideally, that would be enough. If the JavaDoc doesn't make it clear, you probably have to go with the usual suspects: testing, reading the code, asking someone else who might know, and so on.<br /><br />Sorry I can't be more helpful. In an ideal world, everyone would document API thread-safety.Jeremy Mansonhttps://www.blogger.com/profile/04241094734813086257noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-64554763400768211492010-01-11T09:06:22.777-08:002010-01-11T09:06:22.777-08:00How do we know that a class is not Thread safe? Is...How do we know that a class is not Thread safe? Is immutability the only test or are there any other key factors to look for (I am assuming that the class documentation is not mentioning anything about thread safety).Sumanth P Krishnahttps://www.blogger.com/profile/12210750088648280818noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-14052408839363013872010-01-08T12:08:11.469-08:002010-01-08T12:08:11.469-08:00That just bit my ass yesterday. i was making piece...That just bit my ass yesterday. i was making piece of the code working with multi threading, and i started to get sporadic "array Index out of bound exceptions" from Date formatter class. What a piece of turd IBM injected to the JDK. <br /><br />Silly me, i thought i would be ok just making the calls synchronized. Now i switched to Joda time, problem solved. <br />i so wish that Jsr-310afsinahttps://www.blogger.com/profile/07225476124525448066noreply@blogger.comtag:blogger.com,1999:blog-8405107760807432973.post-10978881686297674372010-01-06T23:29:18.392-08:002010-01-06T23:29:18.392-08:00for just this example, the formatter created every...for just this example, the formatter created everytime this method executed. Are we really need to put it to threadlocal??<br /><br />Of course, making formatter global and put in threadlocal run a lot faster.CARFIELDhttps://www.blogger.com/profile/13094952618057460795noreply@blogger.com