Reputation: 97
I'm wanting to read the line of a .txt do the task, and move on to the next line.
To make the program quicker, I want to multi-thread so each thread tries a line.
The problem is, I don't want the thread 1 trying the same line as thread 2.
Any ideas?
Upvotes: 1
Views: 4115
Reputation: 342
I have an idea on what you are trying to achieve but not sure how you will code it. If you can make each thread work on a different set of lines then the threads will not clash. I mean let thread1 read from 1 to 100, thread2 read from 101 to 200 and so on. At the end join the results of all the threads to get the final result. Maybe a Fork/Join operation.
But @Jon comment makes me wary of following such an approach.
"You really don't want multiple threads performing IO here - even if you had multiple independent BufferedReaders, if you're reading from traditional disks you don't want to end up seeking in multiple places..
Upvotes: 0
Reputation: 21
You can read the lines in one thread and dispatch a worker thread for each line:
try (BufferedReader reader = new BufferedReader(new FileReader("my.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
new Runnable() {
void run() {
// do stuff with line
}
}.run();
}
}
Upvotes: 0
Reputation: 1500665
I suggest you have one thread doing all the reading, then put the lines into a producer/consumer queue (e.g. a LinkedBlockingQueue
) - you can then have multiple threads servicing that queue as consumers.
You really don't want multiple threads performing IO here - even if you had multiple independent BufferedReaders
, if you're reading from traditional disks you don't want to end up seeking in multiple places. A producer/consumer queue separates the reading from the handling fairly simply - and makes it easier to test each part in isolation as well.
Upvotes: 5