There are several problems with Java that are just crying out for a multi-threaded solution. Two I've hit recently and solved were:
- File.list has horrible problems if there are many thousands of files in the folder.
- SAXParser.parse is a push parser. Many xml users would prefer a pull parser but would rather not add yet another library.
Both of these can be helped considerably using a simple two-thread solution.
In the case of File.list, run the File.list
in a separate thread with a special FileFilter
that posts all files presented to it to a BlockingQueue
. An iterator on the BlockingQueue
can then be used to deliver the files to the caller. An enhancement to list the directory recursively is an easy addition if the code is written correctly.
The SAXParser.parse can be turned inside-out in a similar way.
I have done both of these quite recently and found the experience very enlightening.