Do you think the method children() below is thread-safe?
Java
import java.util.stream.Stream;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public final class SafeXml {
private final Node node;
SafeXml(final Node node) {
this.node = node.cloneNode(true);
}
public Stream<SafeXml> children() {
NodeList nodes = this.node.getChildNodes();
int length = nodes.getLength();
return Stream.iterate(0, idx -> idx + 1)
.limit(length)
.map(nodes::item)
.map(SafeXml::new);
}
}
Of course, since I’m asking this question, the answer is no. For those who haven’t had the pleasure of working with XML in Java (yes, it’s still alive), the org.w3c.dom package is not thread-safe. There are no guarantees, even for just reading data from an XML document in a multi-threaded environment.