Monday, 15 July 2013

java - SimpleDateFormat ThreadSafe Suggestion-> creating new object is better or ThreadLocal is? -



java - SimpleDateFormat ThreadSafe Suggestion-> creating new object is better or ThreadLocal is? -

i building application in there have format date. formatting using

simpledateformatter class.

as per knowledge there 3 ways utilize class synchronization

1) create local variable

new simpledateformatter("mm/dd/yyyy")

2) utilize synchronized keyword

synchronized(this) { simpledateformatter.format(date); //use static object , format synchronization }

3) utilize thread local variable simple date formatter this

private static threadlocal<simpledateformat> outdateformatholder = new threadlocal<simpledateformat>() { @override protected simpledateformat initialvalue() { homecoming new simpledateformat("mm/dd/yyyy"); }

i building web application, can receive multiple request , @ point formatting date.

now know if have format more 1 time in same thread threadlocal have been improve option.

but according current scenario every thread format date once.

question-> whole question boils situation. improve alternative 1 or 3, since alternative 2 have performance issue.

as have format once,

---------> alternative 1 , alternative 3 same ?, if not, 1 improve in case?

------> there other way create threadsafe not have performance or memory issue(as read in case of threadlocal).

i open suggestion. please share opinions.

thank time.

if situation single thread have format date once, there's no point going threadlocal, can simple create new object every time.

since object creating nevertheless of situation, in case using threadlocal costly , might have memory issue. suggestion -> go new object creation.

i think right synchronization because block threads , since have process such big request of around 10,000 per second.

another alternative utilize apache commons lang's fastdateformat class suggested @duncan.

hope reply looking for...

java multithreading simpledateformat thread-local

No comments:

Post a Comment