The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

387763 (1) [Avatar] Offline
#1
The parallelization error described in this chapter does not occur.

The algorithm described in this book counts words.
Every time a non-space with a space before it is found - the counter is increased.

...
		String dante = " they  had their faces twisted toward their haunches"
				+" and found it necessary to walk backward ";
		Stream<Character> streamDante = IntStream.range(0, dante.length()).mapToObj(dante::charAt);
		WordCounter counter = streamDante.parallel().reduce(new WordCounter(0, false), WordCounter::accumulate, WordCounter::combine);
	}

	static class WordCounter{
		private final int counter;
		private final boolean lastSpace;
		public WordCounter(int counter, boolean lastSpace) {
			this.counter = counter;
			this.lastSpace = lastSpace;
		}
		public WordCounter accumulate(Character c) {
			if(Character.isWhitespace(c)){
				return lastSpace ? 
							this :
							new WordCounter(counter, true);
			}else{
				return lastSpace ? 
							new WordCounter(counter+1, false) :
							this;
			}
		} 
		
		public WordCounter combine(WordCounter w){
			return new WordCounter(counter+w.counter, lastSpace);
		}
		
		public int getCounter() {
			return counter;
		}
	}


Author mentions a parallelization problem:
Evidently something has gone wrong, but what? The problem isn’t hard to discover. Because the
original String is split at arbitrary positions, sometimes a word is divided in two and then
counted twice.


This never happens, when the algorithm is executed on a parallel stream.
The counter is increased, only if a space-nonspace combination is found.
You can not make the algorithm find more words, by dividing any words.
You can only make it find LESS words, by dividing the space-nonspace combinations.

So no words are counted twice after division.