PS: Sorry for my poor english.I can't describe the problem clearly. :(
When I don't use the "lambda method reference" code style in the static block, like:
static{
map.keySet().parallelStream().forEach(e -> {
System.out.println(e);
});
}
then the program running forever, never stop.
But when I change the code to
static{
map.keySet().parallelStream().forEach(System.out::println);
}
then the bug gone.The program can finish immediately.
Just look directly at the code please, I already tried to simplify the code as far as I can.
public class BugSimulate {
static {
init();
}
private static void init() {
Map<Integer, String> map = new HashMap<>();
int i = 0;
map.put(++i, "1");
map.put(++i, "1");
map.put(++i, "1");
map.put(++i, "1");
map.put(++i, "1");
map.put(++i, "1");
map.put(++i, "1");
map.put(++i, "1");
map.put(++i, "1");
// running forever
map.keySet().parallelStream().forEach(e -> {
System.out.println(e);
});
// finish immediately
// map.keySet().parallelStream().forEach(System.out::println);
}
@Test
public void test() {
new BugSimulate();
}
}
but when I change the lambda code
e -> {System.out.println(e);}
to
System.out::println
the program finish immediately
Or I change parallelStream() to normal stream(),Bugs gone.
Or I remove the static blocks,Bugs gone too.
My jdk version is 1.8.0_202
And the OS version is MacOS 10.14.5