Question

While reading this great example about SwingWorker I got a question I would like to ask here:

Assume that the task of searching for a word in text files can be multithreaded, what would be the best way to perform this task in parallel? While searching each file, I need to update a Swing component(JTextarea in this example). Also, I need to update the Component (JTextarea) when all the threads/tasks are completed.

For those who do not want to read the post:

In a nutshell, the background task is to search for a given word in all text files present in a given directory. The search is performed sequentially reading one file at a time. The goal is to search in parallel.

Was it helpful?

Solution

You may be able to use the approach suggested in this example: a Supervisor worker waits for a CountDownLatch shared by multiple subsidiary worker threads.

OTHER TIPS

First demonstrate that the task is actually CPU bound and not I/O bound. In other words, is it faster for two threads to read two files in parallel than for one thread to read two files in series? Depending on the storage media, the sizes of the files, their physical locations on the disk, and many other things, two threads could actually be slower.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top