Neither
TransformerFactory
nor
Transformer
is guaranteed to be
thread-safe.
Simplest solution is just to give each separate thread
its own TransformerFactory
and
Transformer
objects.
The Templates
class represents a
parsed stylesheet. It creates new
Transformer
objects on demand very quickly.
Templates
is thread-safe.
TransformerFactory xformFactory = TransformerFactory.newInstance(); Source xsl = new StreamSource("stylesheet.xsl"); Templates templates = xformFactory.newTemplates(xsl); ... while (true) { InputStream in = getNextDocument(); OutputStream out = getNextTarget(); Source request = new StreamSource(in); Result response = new StreamResult(out); Transformer transformer = templates.newTransformer(); transformer.transform(request, response); }