开发者

Completely stumped on a Java problem

Please Note: I am not "looking for teh codez" - just ideas for algorithms to solve this problem.

This IS a homework assignment. I thought I was in the home stretch, about to finish it out, but the last part has absolutely stumped me. Never have I been stuck like this. It has to do with threading in Java.

The Driver class reads a file, the first line indicates the number of threads, second line is a space delimited list of file names for each thread to read from. Each thread is numbered (0 - N), N being the total number of files. Each thread reads the file specified, and outputs to a file named t#_out.txt where # is the threads index.

After all of this is done the Driver thread must:

After all threads finish execution, the program Driver.java opens all output files t#_out.txt, reads a line from each file, and writes the line to an output file out.txt.

Example of the out.txt:

MyThread[0]: Line[1]: Something there is that doesn't love a wall,

MyThread[1]: Line[1]: HOG Butcher for the World,

MyThread[2]: Line[1]: I think that I shall never see

MyThread[0]: Line[2]: That sends the frozen-ground-swell under it,

MyThread[1]: Line[2]: Tool Maker, Stacker of Wheat,

MyThread[2]: Line[2]: A poem lovely as a tree.

MyThread[0]: Line[3]: And spills the upper boulders in the sun,

MyThread[1]: Line[3]: Player with Railroads and the Nation's Freight Handler;

MyThread[2]: Line[3]: A tree whose hungry mouth is prest

My problem is: What kind of loop structure could I setup to do this? Read a line from t1_out.txt, write to out.txt, read line from t2_out.txt, write to out.txt, read line from tN_out.txt, write to out.txt? How do I know when one file has reached the end?

Ideas:

  1. Use a while(!done) loop t开发者_如何学Pythono continue looping until each scanner is done. Keep track of an array of booleans indicating whether or not the Scanner is done reading its file. The Scanners would be in an array as well. The problem with this is how do I tell when ALL are done, to finish my infinite loop? In each iteration see if booleans[i] is done and if not then done = false? No good.

  2. Just read every files lines into its own String[] array. Then figure out a loop to alternate the writing to the out.txt. Problem with this is what happens when I hit array index out of bounds? Also this is not in the specs, it says to read a line, and write a line.

EDIT: The solution was to create an allFilesReachedEOF() method which has an initial boolean of true. It then loops through each one, and if ANY have another line to read, sets the return condition to false. This was my while loops condition: while (!allFilesReachedEOF()).

My problem was that I was trying to control the loop from within the loop. So if a file had another line it would continue, but if ANY file EOF'd, the loop would stop.

Thanks for the help!


You could do a while with a condition that not all the files have reached EOF. Then you iterate through all the files, and for those that haven't reached EOF, you read the next line and write it to your output file. As you go, you update your condition variable for the "while" loop.

Is this what you're looking to do?


It sounds like you could use a Queue to achieve this. Add each t#_out.txt's input to the Queue then implement a loop in which you read a line from the polled input and write it to your output. As long as the read line isn't EOF, re-add the input to the Queue. When the Queue is empty, break out from the loop.

Also I recommend a BufferedWriter for the output, which you flush() at the end so that the actual writing only occurs once.


Here are the main points:

  • Create a class that implements Runnable whose run() method does what you need one thread to do. It'll likely need fields for threadNumber and filename. The run method should make sure to close() the output streams of the output files
  • For each thread you need to create, instantiate one of your class (giving it the filename (and other) data it needs) and pass it into the constructor of Thread. Keep a reference to the Thread objects
  • Call the start() method on all the threads
  • Call the join() method on all the threads (join waits for the thread to finish)
  • Do your final Driver task of opening the output files


This can be done by using a do - while "exit condition" loop and an inner for loop for iterating through the output files. Set the exit condition to true before the start of the for loop, and reset it within the for loop if you get at least a line from any of the files.

Files that have reached eof will continue to be read, but will not return any lines. You can choose to print blank lines for these or just skip them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜