完全被一个Java问题难住了
请注意:我不是在“寻找代码”——只是解决这个问题的算法的想法。
这是一项家庭作业。我以为我已经到了最后冲刺阶段,即将完成它,但最后一部分绝对难倒了我。我从来没有像这样被困过。它与 Java 中的线程有关。
Driver 类读取文件,第一行指示线程数,第二行是每个线程读取的以空格分隔的文件名列表。每个线程都有编号 (0 - N),N 是文件总数。每个线程读取指定的文件,并输出到名为 t#_out.txt 的文件,其中 # 是线程索引。
完成所有这些后,驱动程序线程必须:
所有线程执行完毕后,程序Driver.java打开所有线程 输出文件 t#_out.txt,从每个文件中读取一行,然后写入 行到输出文件 out.txt。
out.txt 示例:
MyThread[0]: Line[1]: 有些东西不喜欢墙,
MyThread[1]:行[1]:世界的 HOG 屠夫,
MyThread[2]: Line[1]: 我想我永远不会看到
MyThread[0]: Line[2]: 发送其下方的冰冻地面膨胀,
MyThread[1]:线路[2]:工具制造者,小麦堆垛机,
MyThread[2]: Line[2]: 一首像树一样可爱的诗。
MyThread[0]: Line[3]: 并在阳光下洒下上面的巨石,
MyThread[1]:线路[3]:铁路玩家和国家货运处理者;
MyThread[2]: Line[3]: 一棵树,其饥饿的嘴是 prest
我的问题是:我可以设置什么样的循环结构来做到这一点?从 t1_out.txt 读取一行,写入 out.txt,从 t2_out.txt 读取行,写入 out.txt,从 tN_out.txt 读取行,写入 out.txt?我如何知道一个文件何时到达末尾?
想法:
使用 while(!done) 循环继续循环,直到每个扫描仪完成。跟踪布尔值数组,指示扫描程序是否已完成读取其文件。扫描仪也将位于一个阵列中。问题是我如何判断所有内容何时完成,以完成我的无限循环?在每次迭代中查看 booleans[i] 是否完成,如果没有则完成 = false?不好。
只需将每个文件行读入其自己的 String[] 数组中。然后找出一个循环来交替写入 out.txt。问题是当我超出数组索引时会发生什么?另外,这不在规范中,它说读取一行,然后写入一行。
编辑:解决方案是创建一个 allFilesReachedEOF() 方法,其初始布尔值为 true。然后它循环遍历每个行,如果 ANY 还有另一行要读取,则将返回条件设置为 false。这是我的 while 循环条件: while (!allFilesReachedEOF())。
我的问题是我试图从循环内部控制循环。因此,如果文件有另一行,它将继续,但如果任何文件 EOF,则循环将停止。
感谢您的帮助!
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:
Use a while(!done) loop to 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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以执行一段时间,但条件是并非所有文件都已达到 EOF。然后迭代所有文件,对于那些未达到 EOF 的文件,读取下一行并将其写入输出文件。在此过程中,您可以更新“while”循环的条件变量。
这是您想要做的吗?
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?
听起来您可以使用 Queue 来实现这一点。将每个 t#_out.txt 的输入添加到队列,然后实现一个循环,在该循环中从轮询的输入中读取一行并将其写入输出。只要读取的行不是 EOF,就将输入重新添加到
队列
。当Queue
为空时,退出循环。我还推荐使用 BufferedWriter 作为输出,您在最后
flush()
以便实际写入只发生一次。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 theQueue
. When theQueue
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.以下是要点:
run()
方法可以完成您需要一个线程执行的操作。它可能需要threadNumber
和filename
字段。 run 方法应确保 close() 输出文件的输出流Here are the main points:
run()
method does what you need one thread to do. It'll likely need fields forthreadNumber
andfilename
. The run method should make sure to close() the output streams of the output files这可以通过使用 do - while“退出条件”循环和用于迭代输出文件的内部 for 循环来完成。在 for 循环开始之前将退出条件设置为 true,如果从任何文件中至少获得一行,则在 for 循环中重置它。
已达到 eof 的文件将继续读取,但不会返回任何行。您可以选择打印这些空行或跳过它们。
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.