9
votes

java.text.SimpledateFormat pas thread

Synchronization

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
The above line is mentioned in the JavaDoc of SimpleDateFormat class.Does it mean that we should not create the SimpleDateFormat objects as Static.And If we create it as static, so wherever we are using this object we need to keep it in Synchronised Block.

1 commentaires

Le nouveau java.time.format.dateTimeformatter (à partir de Java 1.8) "est immuable et thread-coffre-fort". Donnez un coup d'oeil à: docs.oracle.com / Javase / 8 / Docs / API / Java / Time / Format / ...


3 Réponses :


24
votes

C'est vrai. Vous pouvez trouver déjà des questions concernant ce numéro sur Stackoverflow. J'utilise pour le déclarer comme threadlocal : xxx

et dans le code: xxx


0 commentaires

16
votes

Oui SimpleDateDeformat n'est pas sûr du fil et il est également recommandé lorsque vous analysez la date d'analyse de la manière synchronisée. xxx

une autre solution est sur http: // code.google.com/p/safe-simple-date-format/downloads/list


0 commentaires