It’s obvious that you don’t put any kind of heavy computation into a static initialiser. Anything that uses parallel stream would be by definition heavy.
You can't judge how heavy a computation is by looking at the code in your static initializer. You might just do "new Foo()" and THAT inside uses parallel streams for some reason.
If they wanted to restrict calling arbitrary code... they should've restricted calling arbitrary code.
I didn’t test it, but I believe that there is no problem with calling another class that creates threads. The problem here is about calling the same class’s static methods from another thread from the static initializer. Which you can’t do by a trivial new Foo(), only by this already red flag raising line in the video. (You can fall into this trap by new Foo() that awaits a lambda though, I believe . But I would yet again question that code)
Nonetheless, a compiler warning would be appreciated if it can prove in this specific case that a deadlock can arise.
35
u/AndrewHaley13 Mar 21 '21
Well, this kind of edge case really needs to be fixed. It's far from obvious that lambda streams shouldn't be used in static initializers.