Skip to content

Instantly share code, notes, and snippets.

@N02870941 N02870941/Threads.java
Last active Feb 15, 2019

Embed
What would you like to do?
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.
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
You can’t perform that action at this time.