• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SimpleDateFormat

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since SimpleDateFormat and DateFormat are not thread safe what is the faster way to be thread safe?
Create a new instance for each thread
-or-
place a synchronized block around the static shared DateFormat
?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robert,


Create a new instance for each thread
-or-
place a synchronized block around the static shared DateFormat


I'm not exactly sure what you're getting at here or maybe I just don't understand your design. If you create a new instance (of DateFormat?) for each thread then obviously every instance is safe provided that no other thread is allowed to access (modify) that instance, but can your design give up identity of a single object which is the whole point of thread-safety? Of course if the DataFormat object is declared static then there can only be one (class) instance so you must synchronize on methods that will access it if you don't want it corrupted.
I don't advise using synchronized blocks on objects other than "this" because they are prone to deadlock. You're better off synchronizing methods instead because you're going to take a perfomance hit either way and probably can't justify using blocks instead of whole methods.
Hope this helps
Michael Morris
SCJP
 
Robert Gagliardo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a utility class that has several static SimpleDateFormats. Methods within the class parse/format on them.
I could synchronize the methods, but because the methods are static, won't that make the whole class synchronized?
-or-
I could just write a simple method that returns new instances of SimpleDateFormats based on a given format parameter.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robert,
Regardless of whether the data you're trying to protect is static or not, when you synchronize, you lock the whole object. All objects have a monitor which you can think of as a door with a single key on the doormat. When a thread wants to access a synchronized method on that object, it grabs the key and locks the door from the inside preventing all other threads from entering. Whatever thread has the key keeps it until it finishes all synchronized methods while it has the key or calls wait() on the object and enters the objects pool of waiting threads where it is ineligible for scheduling. If it calls wait() it remains inactive until another thread calls either notify() or notifyAll() on the same object and is lucky enough to be arbitrarily chosen to once again receive the object lock.
Now if performance is truly an issue, and your desgin can tolerate creating new instances on the fly (I'm assuming private access on the protected data and no publicly available means to get to that data) then that is probably what you want to do.
Hope this helps
Michael Morris
SCJP
 
Robert Gagliardo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael, thanks for your responses
 
reply
    Bookmark Topic Watch Topic
  • New Topic