Java tutorial
//package com.java2s; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import javax.annotation.concurrent.GuardedBy; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathFactory; public class Main { /** * Lock to guard {@code newInstance} methods of {@code XPathFactory} class * which is documented to be neither thread-safe and re-enterant. * * @see javax.xml.xpath.XPathFactory */ private final static Lock xpathFactoryLock = new ReentrantLock(); @GuardedBy("xpathFactoryLock") public static XPath createNewXPathInstance() { XPathFactory factory = null; xpathFactoryLock.lock(); try { factory = XPathFactory.newInstance(); } finally { xpathFactoryLock.unlock(); } return factory.newXPath(); } }