Created
March 19, 2020 20:50
-
-
Save jasontedor/6cfd7bff02dc4f3f81e736343b74c8af to your computer and use it in GitHub Desktop.
Scratch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
class Scratch { | |
public static void main(String[] args) { | |
String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; | |
ExecutorService executorService = Executors.newFixedThreadPool(4); | |
doIt(letters, new Processor(executorService), String::toUpperCase); | |
executorService.shutdown(); | |
} | |
public static void doIt(String[] letters, Processor processor, Function<String, String> function) { | |
for (final String letter : letters) { | |
processor.process(letter, function, System.out::println); | |
} | |
} | |
static class Processor { | |
final ExecutorService executorService; | |
Processor(ExecutorService executorService) { | |
this.executorService = executorService; | |
} | |
public Future<?> process(String letter, Function<String, String> function, Consumer<String> handler) { | |
return executorService.submit(() -> handler.accept(function.apply(letter))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment