Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multithreaded XML file parsing?



Saad Mahamood wrote:

One area in my code that could benefit from threading is the following:


File[] dirChildren = gmlDir.listFiles(filter);
for(File aFile : dirChildren) {
System.out.println("Got here!");
//Iterate through the list of files and start loading GML files:
GMLThread thread = new GMLThread(aFile);
thread.run();
try {
thread.join();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("Got here! 2");
}


I'm not loading one XML file, but several in a directory. However, I
can't figure out how to iterate through the File arraylist and start
several parsing threads, whilst making the main application thread
wait until they've have all completed? The current code above only
makes the "for each" loop wait until the thread its spawned has
completed, which isn't the behaviour I want.

Note that your code above does not spawn any new threads (unless you've overridden Thread.run to do something very arcane). You create a Thread object, but calling its run method does not spawn a new thread, it runs the thread body method in-line in the current thread. To spawn a new thread and start it running requires you call Thread.start from the invoking thread. I'd expect the code as quoted above to hang on the call to Thread.join, as Thread thread has never been started.


I also believe that the code above will leave a dangling reference to Thread thread, effectively leaking it. The ThreadGroup that the spawned Thread instance belongs to keeps a reference to it, and doesn't discard it until the Thread exits. If the Thread is never started, it never exits and the reference is never discarded (this was true as of Java 1.4 -- I'm not sure if it is still true in J2SE5).

As others have pointed out, the way to do what you want is to record each Thread in an array or collection as you start it, and once all have been started then iterate over them calling join. This will block the invoking Thread until all spawned Threads have completed. It is considered bad form to do this on the Swing Thread -- your GUI will be "dead" until all the Threads have terminated. If the function is modal (i.e. the user cannot use the GUI until the operation has completed), then use another mechanism to lock-out your GUI, e.g. set the enabled property of your window to false while the threads are working, and set it back to true when completed (this will automatically disable/enable all nested components).

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.