Skip to content

Instantly share code, notes, and snippets.

@N02870941
Last active February 15, 2019 09:37
Show Gist options
  • Save N02870941/a9fbe7ddb0ec90246fd9cc40d715733f to your computer and use it in GitHub Desktop.
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.
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