Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of SimpleDateFormat Thread unsafe in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "java SimpleDateFormat thread unsafe example analysis", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "java SimpleDateFormat thread unsafe example analysis" this article.

Class java.text.SimpleDateFormat is mainly responsible for date conversion and formatting. Such as:

Write a simple test program, such as:

However, in a multithreaded environment, using the class java.text.SimpleDateFormat can easily lead to incorrect data conversion and processing, because the class is not thread-safe.

We can see the following sentence from the javadoc description of the SimpleDateFormat class:

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.

Date formats is not synchronized.

It is recommended that you create a separate format object for each thread.

If multiple threads access a format concurrently, be sure to implement synchronized externally.

In other words, in multithreading, we need to do some additional protection to ensure that it is handled correctly, otherwise it is not safe. Next, let's take a look at what happens in multithreading.

Thread unsafe exampl

The running result mainly contains the following errors:

No exception, date parsing error

There's an anomaly, java.lang.NumberFormatException.

Such as

Again such as

So the question is, how to ensure normal operation?

Solution method

In fact, you can see that there are ways to deal with it in SimpleDateFormat's javadoc.

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.

Next, the relevant solutions are given from this description.

(1) create a new SimpleDateFormat object each time

Modify the SimpleDateFormatExample class, such as:

Execute the above Main.java class to get the correct result:

(2) when accessing format, add synchronized

Modify the SimpleDateFormatExample class, such as:

Or add synchronized decorations before using the methods of the format object, such as:

Similarly, execute the above Main.java class to get the correct result:

(3) use TheadLocal

Modify the SimpleDateFormatExample class, such as:

Similarly, execute the above Main.java class to get the correct result:

(IV) use FastDateFormat

The FastDateFormat class is under the Apache Common Langs package

This class is thread safe.

If it is a Maven project, the dependency package is added as follows:

Modify the SimpleDateFormatExample class, such as:

The complete classes are:

Similarly, execute the above Main.java class to get the correct result:

(v) use Joda Time

The DateTimeFormatter class is thread-safe under the Joda-Time package.

If it is a Maven project, the dependency package is added as follows:

Modify the SimpleDateFormatExample class, such as:

Similarly, execute the above Main.java class to get the correct result:

The above is all the contents of the article "sample Analysis of SimpleDateFormat Thread unsafe in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report