Submit every line, queue every Future
Submit every line, queue every Future
Answer
Queue<Future<Hit>> pending = new ArrayDeque<>(); for (String line = in.readLine(); line != null; line = in.readLine()) { pending.add(pool.submit(new ResolveHit(line))); } while (!pending.isEmpty()) { Hit hit = pending.remove().get(); // blocks only on THIS record System.out.println(hit.host() + " " + hit.bytes()); }
Two loops again: the first fills the pool and never waits, the second restores input order. Merging them into one loop would submit a line, wait for it, then submit the next — the serial program with extra ceremony. Draining with `remove()` also lets each finished `Hit` be collected as you go, which a `for` loop over the queue cannot do.
Harold 4e ch4 §Processing Web Server Logfiles; Harold 4e ch3 §Thread Pools and Executors