Last active
February 15, 2019 09:37
-
-
Save N02870941/a9fbe7ddb0ec90246fd9cc40d715733f to your computer and use it in GitHub Desktop.
A simple Java class with a one-liner that utilizes Java 8 lambdas to concisely demonstrate how to use threads and accomplish concurrency in Java.
This file contains 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.stream.IntStream; | |
class Threads { | |
public static void main(String... args) { | |
IntStream | |
.range(0, 10) | |
.mapToObj(i -> new Thread(() -> System.out.println(Thread.currentThread().getName()))) | |
.forEach(thread -> thread.start()); | |
} | |
} | |
// Output: | |
/* | |
Thread-0 | |
Thread-3 | |
Thread-1 | |
Thread-2 | |
Thread-4 | |
Thread-7 | |
Thread-5 | |
Thread-6 | |
Thread-9 | |
Thread-8 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment