XeroxDucati
XeroxDucati

Reputation: 5200

Thread contention on JsonConvert calls?

Has anyone come across thread contention issues using JsonConvert in JSON.NET? I've got a large number of threads calling JsonConvert.Deserialize concurrently, and they appear to be blocking each other. VS Profiler is showing me that all my concurrency issues are buried in Newtonsoft.*. Is this a known thing? Is there something I should be doing to make the deserialize calls run in parallel?

I realize any one call is a forward-only op and won't thread itself, but why would independent deserializations contend?

Upvotes: 2

Views: 2201

Answers (1)

user3818435
user3818435

Reputation: 727

If you are calling a non-async method like the following:

var obj = JsonConvert.DeserializeObject<T>(jsonValue);

It is a blocking call. Consider using the async version as follows:

 var task = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(jsonString));
 var value = await task;  

It turns out JsonConvert.DeserializeObjectAsync() isn't truly async as mentioned by Stephen and shown here(http://james.newtonking.com/json/help/index.html?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObjectAsync_1.htm)

Hope this helps.

Happy coding!

cleankoder

Upvotes: 1

Related Questions