I have to make a program that searches through a bunch of lines from a file and tries to find a given substring. If it finds it, it prints out the line. Each line that I read is created as a thread and each thread searches a single line of the file. Thats not a problem so far. What I need the program to do is print the end results (the line of text) in the same order that the threads were created. I.E. thread 6 shouldnt be printing before thread 2. Its fine that the threads run at the same time, the printing order just needs to be maintained. I can not use the join method as I don't want the next one to wait for the other to finish entirely before starting, I do want them to be running at the same time. Any suggestions on doing this? Also, the file can have any number of lines so I cant hardcode the number of threads.
THE THREADS SHOULD DO THEIR OWN PRINTING. THE MAIN DOES NOT DO THE PRINTING.
I can not use the join method as I don't want the next one to wait for the other to finish entirely before starting, I do want them to be running at the same time.
You could make them populate an array (where each one "knows" the element to populate to start with, as part of its initial state), then join on all the threads so that you wait for them all to finish, and then just print out the contents of the array in order.
See more on this question at Stackoverflow