This quick and dirty hack demonstrates the threading bug in Namespace.java that I posted earlier today. If I run this several times, it eventually hangs forever.<br><br>By adding the synchronized keyword to the Namespace.getNamespace(...) method, the program seems to always work.<br>
<br><br><br>import org.jdom.Namespace;<br><br>import java.util.concurrent.CountDownLatch;<br>import java.util.concurrent.ExecutorService;<br>import java.util.concurrent.Executors;<br><br>public class JdomThreadBug {<br><br>
public static void main(String[] args) throws InterruptedException {<br> ExecutorService es = Executors.newCachedThreadPool();<br><br> int numThreads = 100;<br> CountDownLatch startLatch = new CountDownLatch(1);<br>
for (int i = 0; i < numThreads; i++) {<br> es.submit(new Namespacer(i * 10, i * 10 + 10, startLatch));<br> }<br><br> startLatch.countDown();<br> System.out.println("*** DONE ***");<br>
es.shutdown();<br> }<br><br> private static class Namespacer implements Runnable {<br> private final int min;<br> private final int max;<br> private final CountDownLatch startLatch;<br><br>
private Namespacer(int min, int max, CountDownLatch startLatch) {<br> this.min = min;<br> this.max = max;<br> this.startLatch = startLatch;<br> }<br><br> public void run() {<br>
try {<br> startLatch.await();<br> for (int i = min; i < max; i++) {<br> String prefix = "p" + i;<br> String uri = "u" + i;<br>
System.out.println("Getting namespace " + prefix + ":" + uri);<br> Namespace.getNamespace(prefix, uri);<br> System.out.println("Got namespace " + prefix + ":" + uri);<br>
}<br> } catch (InterruptedException e) {<br> Thread.currentThread().interrupt();<br> }<br> }<br> }<br>}<br><br clear="all"><br>-- <br>Eric M. Burke<br><a href="http://www.linkedin.com/in/ericburke">http://www.linkedin.com/in/ericburke</a><br>
314-494-3185 (mobile)<br>636-272-3298 (home)<br>