Is it safe to use jsoup someDocument.select(..) from multiple threads or is there some internal state for read operations?
Is it safe to use jsoup someDocument.select(..) from multiple threads or is there some internal state for read operations?
You can call safely Document.select(String cssSelector) from multiple threads even though Document class is not thread-safe. Underlying implementation of .select(String cssSelector) method passes reference to the element that called this method (Document object in this case), but it does not call any method that changes state of the caller.
When you call .select(String cssSelector) you actually call Collector.collect(Evaluator eval, Element root) method, where root instance is a reference to the Document object.
/**
Build a list of elements, by visiting root and every descendant of root, and testing it against the evaluator.
@param eval Evaluator to test elements against
@param root root of tree to descend
@return list of matches; empty if none
*/
public static Elements collect (Evaluator eval, Element root) {
Elements elements = new Elements();
new NodeTraversor(new Accumulator(root, elements, eval)).traverse(root);
return elements;
}
In this method only elements object gets updated.
Document class is not thread-safe?There are a few methods in Document class that allow to change state of the object without any synchronization mechanism, e.g. Document.outputSettings(Document.OutputSettings outputSettings). In best case Document class should be final and immutable so sharing its instance between multiple threads won't be a problem.