VM Crash: Same thread Executor

Alan Bateman Alan.Bateman at oracle.com
Wed Aug 12 09:05:06 UTC 2020


On 11/08/2020 20:28, Thomas May wrote:
> The following code intermittently crashes 16-loom+4-56 (It's crashed on me 3 out of 4 times).
>
> public class Main {
>                  public static void main(String[] args) {
>                                  try (var e = Executors.newThreadExecutor(Thread.builder().daemon(true).virtual(Runnable::run).factory())) {
>                                                  for (int i = 0; i < 10000000; ++i) {
>                                                                  int j = i;
>                                                                  e.submit(()->{
>                                                                                  try {
>                                                                                                  Thread.sleep(1000);
>                                                                                                  System.out.println(j);
>                                                                                  } catch (Exception ex) {
>                                                                                                  throw new RuntimeException(ex);
>                                                                                  }
>                                                                  });
>                                                  }
>                                  }
>                  }
> }
>
I tried your example and it crashes for me intermittently due an an 
underlying OOME that triggers a guarantee. I wasn't able to duplicate 
the crash you are seeing but there have been several fixes in recent 
weeks that may be related to the issue that you are seeing. We need to 
refresh the EA build to pick up these fixes.

Anyway, as Ron said, virtual(Runnable::run) will run on the current 
thread so the expected behavior with the above will be use the main 
thread to run the 10 million virtual threads. These will park in 
Thread.sleep and the main thread will block in close waiting for the 10 
million threads to terminate. The carrier thread used to execute the 
code after Thread.sleep will be a timer thread that unpark the virtual 
thread and you don't want that. There is a warning in the 
virtual(Executor) javadoc to not do this and we might need to make it a 
bit more prominent.

-Alan


More information about the loom-dev mailing list