Java 9 Programming By Example by Peter Verhas

Java 9 Programming By Example by Peter Verhas

Author:Peter Verhas
Language: eng
Format: epub, azw3
Tags: COM051280 - COMPUTERS / Programming Languages / Java, COM051010 - COMPUTERS / Programming Languages / General, COM051300 - COMPUTERS / Programming / Algorithms
Publisher: Packt Publishing
Published: 2017-04-25T10:01:41+00:00


private void startAsynchronousGuessers(

IntervalGuesser[] guessers) {

executorService = Executors.newFixedThreadPool(nrThreads);

for (IntervalGuesser guesser : guessers) {

executorService.execute(guesser);

}

}

private void stopAsynchronousGuessers(

IntervalGuesser[] guessers) {

executorService.shutdown();

guessQueue.drainTo(new LinkedList<>());

}

The code is quite straightforward. The only thing that may need mention is that the queue of the guesses is drained into a collection that we do not use afterward. This is needed to help any IntervalGuesser that is waiting with a suggested guess in hand, trying to put it into the queue. When we drain the queue, the guesser thread returns from the method put in the guessQueue.put(guess); line in IntervalGuesser and can catch the interrupt. The rest of the code does not contain anything that would be radically different from what we have already seen and you can find it on GitHub.

The last question that we still want to discuss in this chapter is how much speed did we gain making the code parallel?



Download




Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Web Analytics