Created
April 7, 2021 13:52
-
-
Save apangin/88133b79b14f0dad6ab2e21cc773cddf to your computer and use it in GitHub Desktop.
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.ArrayList; | |
import java.util.concurrent.locks.LockSupport; | |
import java.util.stream.StreamSupport; | |
public class ParallelStream { | |
public static int[] process(Iterable<Integer> iterable) { | |
return StreamSupport.stream(iterable.spliterator(), true) | |
.mapToInt(i -> { | |
LockSupport.parkNanos(1_000_000); | |
return i; | |
}) | |
.toArray(); | |
} | |
public static void main(String[] args) { | |
ArrayList<Integer> list = new ArrayList<>(); | |
for (int i = 0; i < 1000; i++) { | |
list.add(i); | |
} | |
while (true) { | |
long startTime = System.nanoTime(); | |
process(list::iterator); | |
// process(list); | |
long endTime = System.nanoTime(); | |
System.out.println((endTime - startTime) / 1e6 + " ms"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment