1

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.

Mo3z
  • 2,138
  • 7
  • 21
  • 29

2 Answers2

1

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.

Brodo Fraggins
  • 628
  • 5
  • 14
  • 1
    If the task is to rotate a list of images by 90 degrees. Would that not be faster if multithreaded? You can also take an example where it needs to make some http requests. Searching for a word in a list of files is just an example. My goal was to understand the approach. – Mo3z May 01 '14 at 00:28
  • So is your question whether it's better to use multiple SwingWorkers, an Executor, etc.? – Brodo Fraggins May 01 '14 at 00:40
  • Yes, if I use multiple SwingWorkers or an Executor, how do I send updates or progress to the Component (JTextarea) and how do I know when all the threads are done? – Mo3z May 01 '14 at 00:44
1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045