Why is it always the things that are supposed to be simple that come around and byte you in the behind? In my present (Java) project (a quasi-real-time data aquisition and archiving module), I had a SimpleDateFormat for parsing and formating dates in UTC format. Things were all cute and sweet, until I got more than one client loading the server. Then, all hell broke loose on the dates that got to the tags; here’s an example:
2008-07-14 10:27:25,Posição do elevador A17,43,1
2040-01-31 10:27:00,Freq. oscilacao da plataforma A17,8,1
0037-01-01 00:27:00,Valvula IN 01V001 A50,0,1
2008-07-01 00:00:00,Valvula IN 01V004 A50,0,1
2008-06-30 00:00:00,Pressao Vapor Caldeira A17,1.256,1
2008-01-01 10:00:55,Consumo - Agua da Rede A17,3.74,1
2008-07-14 10:33:58,Posição do elevador A17,26,1
2008-07-14 10:33:58,Freq. oscilacao da plataforma A17,-20.0,2
2008-07-14 10:33:58,Valvula IN 01V001 A50,0,1
This is supposed to be the transition from 2008-07-14 10:33:52 UTC to 2008-07-14 10:33:58 UTC, but as you can see there are a lot of bogus dates in between… the problem was a transient one (and darn difficult to track down), apparently the SimpleDateFormat class is not Thread-safe (I have 7 different work threads on this baby, not counting the dynamic RMI ones of course). Oh, oh, wait, what’s this on the SimpleDateFormat API docs?
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
RTFM situation, I guess. Oh well.