First Crack at Multithreaded Programming

  • Thread starter Thread starter peterpiper
  • Start date Start date
  • Tags Tags
    Crack Programming
AI Thread Summary
Creating a multithreaded program in Java to find a word in a text file requires careful management to preserve the order of returned lines. Starting multiple threads can lead to unordered results, so a strategy is needed to ensure that lines are reported in the correct sequence. One approach is to divide the text into sections for each thread while collecting results in a way that maintains order, such as sorting after collection. Additionally, parallel processing can be utilized for both searching and sorting to enhance performance. Effective implementation of these strategies can significantly improve the efficiency of the program while keeping the output organized.
peterpiper
Messages
12
Reaction score
0

Homework Statement



Create a multithreaded program (Java) that finds a word in a text file and reports the lines one which the word appeared. Word lines should be reported in the order in which they appear.

Homework Equations





The Attempt at a Solution



I know that I can just start up a bunch of threads to run over different parts of the text file,, but that gives the potential that the lines are going to be returned in all sorts of messy errors. Any suggestions or help materials that could help me preserve the order of the returns while also managing to increase speed over the single-threaded program?
 
Physics news on Phys.org
You can give each thread a small part of the text, so all threads run over "some fraction of the first n% of the text" in parallel. Collect the results, sort them*, print them, continue with the next n% of the text (can be done in parallel to the sorting of course).

*trivial with a good line distribution
 
Back
Top