Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsoup thread safety

Tags:

java

jsoup

Jsoup parse(String html) is not ducumented as thread safe. How do you parse multiple documents with Jsoup concurrently? Thanks

like image 902
Sam Adamsh Avatar asked Nov 18 '12 23:11

Sam Adamsh


2 Answers

By using Joup.parse.

As long as you're not working on the same document, it creates new object internally.

https://groups.google.com/forum/?fromgroups=#!topic/jsoup/QIij7DEhj8E

This comes up from time to time; it's probably worth filing a documentation issue against this.

like image 88
Dave Newton Avatar answered Sep 29 '22 21:09

Dave Newton


Looking at the Jsoup.java source code it doesn't have any state and both parse() methods are delegating to Parser.parse() that is internally creating and delegating to a TreeBuilder.parse(). Both Jsoup and Parser classes have no state and are only holding static methods. TreeBuilder class though has a state and seems to be doing all the work but it's created from within a method therefore the whole operation is thread-safe by virtue of stack/thread confinement.

like image 37
dimitrisli Avatar answered Sep 29 '22 23:09

dimitrisli