1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: First Crack at Multithreaded Programming

  1. Mar 4, 2013 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant equations

    3. 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?
  2. jcsd
  3. Mar 5, 2013 #2


    User Avatar
    2017 Award

    Staff: Mentor

    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
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted