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


I am creating an application in which I have a format date. For formatting I am using

  SimpleDateFormatter class.   

According to my knowledge there are three ways to use this class with synchronization

1) Create a local variable

  new SimpleDateFormatter ("MM / dd / yyyy")   

2) Use synchronized keywords

  synchronize (this) {simpleDateFormatter.format (date); // Use the static object and then format it with synchronization)   

3) Use the thread local variable with the ordinary date format such as

  Private Stable Thread Local & lt; SimpleDefamate & gt; OutDateFormatHolder = New Thread Local & lt; Simplified Format & gt; () {@ Override Protected Simple Data Formatting Value () {New Simple Format ("MM / DD / Yay"); }   

I am creating a web application, where I can get multiple requests and at some point I will date the formatting.

Now I know that if I have more than one format format in the same thread then ThreadColok would be a better option.

But according to the current scenario, each thread will form a date once.

Question-> The whole question bursts for the situation. What would be a better option 1 or 3, because option 2 would be the issue of performance

As I have to format only once,

--------- & Gt; What are Option 1 and Option 3? If they are not, who is better in my case?

------ & gt; Is there any other way to make this thread in which there will be no display or memory problem (as read in case of Threadlock).

I am open to suggestions. Please share your opinion.

Thank you for your time.

If your position is that the single thread has to be formatted once only, then any threadlocal Not being able to, you can create new objects all the time.

Since the object is causing the condition, using this threadlocal in this case would be expensive and memory problems may be my suggestion - & gt; Go for new object creation.

I think that you are right about the synchronization because it will stop threads and when you have to act on such a large request of about 10,000 per second.

Another option would be to use the Fastcardtech Format Class of Apache Commons Lang as suggested by Dunken.

Hope this answer is looking for you ...

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -