Skip to content

Instantly share code, notes, and snippets.

@MrChebik
Last active August 26, 2017 13:45
Show Gist options
  • Save MrChebik/cc70c5f29e17ef9b4bf0ddde93aa2463 to your computer and use it in GitHub Desktop.
Save MrChebik/cc70c5f29e17ef9b4bf0ddde93aa2463 to your computer and use it in GitHub Desktop.
public class Main {
private static final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
public static void main(String[] args) {
Stream.of("WELL, PRINCE, so Genoa and Lucca are now " +
"just family estates of the Buonapartes. But I " +
"warn you, if you don't tell me that this means " +
"war, if you still try to defend the infamies and " +
"horrors perpetrated by that Antichrist I real- " +
"ly believe he is Antichrist I will have nothing " +
"more to do with you and you are no longer my " +
"friend, no longer my 'faithful slave,' as you " +
"call yourself! But how do you do? I see I have " +
"frightened you sit down and tell me all the " +
"news.").map(e -> {
e = e.trim();
List<MultiThread> multiThreads = new ArrayList<>();
int deliver = e.length() / NUMBER_OF_CORES;
int delivered;
for (int i = 0; i < NUMBER_OF_CORES; i++) {
delivered = deliver * i;
multiThreads.add(new MultiThread(
e.substring(delivered, i != NUMBER_OF_CORES - 1 ? deliver * (i + 1) : e.length()),
(delivered > 0 && e.charAt(delivered) != ' ') && (e.charAt(delivered - 1) != ' ' || e.charAt(delivered + 1) != ' ')));
}
multiThreads.forEach(Thread::start);
multiThreads.forEach(handlingConsumerWrapper(Thread::join, InterruptedException.class));
List<String> returnedList = new ArrayList<>();
multiThreads.forEach(multiThread -> {
if (multiThread.isSplitLeft) {
int lastElem = returnedList.size() - 1;
List<String> words = multiThread.getReturnedWords();
returnedList.set(lastElem, returnedList.get(lastElem) + words.get(0));
returnedList.addAll(words.subList(1, words.size()));
} else {
returnedList.addAll(multiThread.getReturnedWords());
}
});
return returnedList;
}).forEach(System.out::println);
}
private static class MultiThread extends Thread {
private List<String> returnedWords;
private String words;
boolean isSplitLeft;
MultiThread(String words, boolean left) {
this.returnedWords = new ArrayList<>();
this.words = words.trim();
this.isSplitLeft = left;
}
public void run() {
int startAt = 0;
for (int i = 0; i < words.length(); i++) {
if (words.charAt(i) == ' ') {
returnedWords.add(words.substring(startAt, i));
startAt = i + 1;
}
if (i == words.length() - 1) {
returnedWords.add(words.substring(startAt, i + 1));
}
}
}
List<String> getReturnedWords() {
return returnedWords;
}
}
@FunctionalInterface
private interface ThrowingConsumer<T, E extends Exception> {
void accept(T t) throws E;
}
private static <T, E extends Exception> Consumer<T> handlingConsumerWrapper(
ThrowingConsumer<T, E> throwingConsumer, Class<E> exceptionClass) {
return i -> {
try {
throwingConsumer.accept(i);
} catch (Exception ex) {
try {
System.err.println("Exception occurred : " + exceptionClass.cast(ex).getMessage());
} catch (ClassCastException ccEx) {
throw new RuntimeException(ex);
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment