This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to synchronized simpledateformat  RSS feed

 
punna Kumar
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
here my code is look like this
-------------------------------
private static final SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd");

private Element getElement(Element pRootNode) throws Exception {
Date dt = new Date();

// Date and Time Format
String lDate = dateFormat.format(dt);
String lTime = timeFormat.format(dt);
...................
//remaining code

----------------------------

how i can make above code to thread safe.


Rgds,
rkcoollife
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way would be to wrap the call to getElement in a synchronized block:

synchronized (dateFormat) {
...
... getElement(...);
...
}

It's probably better to move the calls to format into their method, and just synchronize, so that the whole getElement isn't synchronized.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!