SimpleDateFormat 2005-09-21 - By Mike New
Back > Frans, > The DateFormat class is not threadsafe and making only one instance of > dateFormat for the whole class will cause threading issues. Any instance > of DateFormat or SimpleDateFormat should be method-local > > -Richard
Or synchronize it. From the javadoc:
"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 externally."
So if you synchronize the call to the format() method, you should be fine. On the other hand, creating a new one for each instance is not a large overhead. Unless you'll be creating a LOT of them...
My $0.02.
Mike > Frans Verhoef wrote: >> Hi, >> Make dateFormat a final static variable. This would create only one >> instance of dateFormat for the whole class. >> >> Cheers, >> Frans >> >> On Tue, 20 Sep 2005 17:36:26 -0600, SUBSCRIBE EJB-INTEREST anonymous >> wrote: >> >>> Hi, >>> Is there a better way to do the below in a multithreaded >>> application? >>> >>> java.text.SimpleDateFormat dateFormat = new >>> java.text.SimpleDateFormat ("MMddyyyy"); String formatedString = >>> dateFormat.format(new Date()); >>> >>> I have the below code within a method in a class. >>> >>> Since, an instance of java.text.SimpleDateFormat will be created >>> for every call of the method, I was wondering this would create lot >>> of objects in heap. >>> >>> >>> Thanks in advance, >>> >>> ====================================================================== >>> ===== To unsubscribe, send email to listserv@(protected) and >>> include in the body of the message "signoff J2EE-INTEREST". For >>> general help, send email to listserv@(protected) and include in >>> the body of the message "help". >>> >> >> =========================================================================== >> To unsubscribe, send email to listserv@(protected) and include in the >> body >> of the message "signoff J2EE-INTEREST". For general help, send email to >> listserv@(protected) and include in the body of the message "help". >> >> >> >> > > =========================================================================== > To unsubscribe, send email to listserv@(protected) and include in the > body > of the message "signoff J2EE-INTEREST". For general help, send email to > listserv@(protected) and include in the body of the message "help". >
-- ---- ---- ------ _/ /// // -- ---- ---- ---- --
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|