2009/5/11 David kerber <dcker...@verizon.net>:
> This is related to the performance issues discussed in the thread
> "Performance with many small requests".
>
> When I reworked my servlet to synchronize only on pieces that needed to be
> synchronized, rather than on the entire request processing routine, I am now
> throwing an exception when parsing a string into a java.util.Date variable.
>  It only happens occasionally, maybe once every few dozen to a hundred or so
> requests, and I can't figure out why it doesn't work all the time.
>
> Declared at the class level, I have:
>
>   private static final SimpleDateFormat    sdfFullDateTime = new
> SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
>
>
> Then in the request processing method, I have
>
>   dateTimeStr = dateStr + " " + timeStr;
>   try {
>       dataDate = sdfFullDateTime.parse( dateTimeStr );
>   } catch ( Exception e ) {
>       writeLog( "Unable to parse dataTime string: '", dateTimeStr + "': " +
> e );
>   }
>
>
> (the try/catch is there only for debugging this issue), and in the log I'm
> seeing:
>
> 2009-05-11 09:19:54: Unable to parse dateTime string: ':  '2009-05-11
> 09:19:37': java.lang.NumberFormatException: For input string: ""'
>
>
> Which I don't understand at all; dateDate (java.util.Date), dateStr
> (String), timeStr (String) and dateTimeStr (String) are all declared in the
> processing method, NOT at the class level.
>
> Maybe I should move the declaration of the SimpleDateFormat into the
> processing method?  Or synchronize the date parse?
>
>
> I'm kind of lost here; any help appreciated!!
>
> Dave
>

As the JavaDoc says
http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html

"Date formats are not synchronized. It is recommended to create
separate format instances for each thread"

You may either create a new instance of SimpleDateFormat each time, or
add a synchronization around that part of code, or use a ThreadLocal
(though ThreadLocal has its own caveats as the threads belong to
Tomcat and are shared among applications).

Without profiling you would not know which one of the ways I am
mentioning is faster.

Maybe I would start with creating a new instance each time, as GC for
short-living objects seems to be cheap in recent JREs. Though without
profiling you will not get the numbers.

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to