First twoArticleAfter learning about the basic usage of Jackson, I still remember the first article, which is so troublesome for me to get an objectmapper object.
. We also use the singleton mode to obtain the result. Why is it not new? Let's look at a piece of data first.
I will convert testvo in our second article to jsoncommonutil. getmapperinstance (false). The average execution time of five times is 210 milliseconds, and the value of false is 9991.6 milliseconds after true.
I don't want to test its performance here (I will write an article dedicated to testing the performance of several JSON class libraries later that will be involved at that timeJSON-lib,Jackson, Gson ...... And other common class libraries
), And my machine is not powerful. I mainly want to explain that the performance difference between New objectmapper () and global use is roughly47Times. What is the concept of 47 times the world of money at this time? How many times does a system need to be converted!
You may have said that you are the only one in the world. Why waste time writing an article. Don't worry. Let me explain it to you! After all, it is global. So what will happen in the case of multithreading and concurrency? Will it disrupt our data? The data cannot be messy. If you have a million deposit bank system, you may cry at a very low pace!
Next, let's take a look at Jackson's official explanation of objectmaper: Jackson'sOrg. codehaus. Jackson. Map. objectmapper"Just works" for mapping JSON data into plain old Java objects ("pojos"). Source: http://wiki.fasterxml.co/JacksonInFiveMinutes
Java Code
- Objectmapper mapper =NewObjectmapper ();// Can reuse, share globally
Show off my poor English:
Objectmapper is only used to map JSON to pojo and can be reused globally.
Since we have said that we can only map JSON to pojo for global use, do we need to study it? I feel necessary! Because I do not know whether it is thread-safe. After all, data is the first. Next, let's test whether it is thread-safe in extreme cases.
My test logic is as follows: when multiple thread threads 1mapper have been mapped to a certain object, let the thread sleep and then thread 2 execute. Check whether the Conversion Result of thread 2 is correct. Although it is a bit inaccurate, it can also be seen whether it is thread-safe. Because there are too many codes that are currently being sorted out, I will paste the code after finishing the sorting.