r/java • • Mar 21 '21

Java lambdas can deadlock parallel class initialization

https://www.youtube.com/watch?v=_2hZf1kw4A4
230 Upvotes

46 comments sorted by

View all comments

Show parent comments

73

u/murkaje Mar 21 '21

There's nothing to fix as it's documented behavior. What needs to be improved is educating newer java developers that static initializer is not some nice place to dump code, but a critical part of class loading and initialization flow and that before classes are fully initialized many restrictions are placed on them and the performance is poor.

The topic keeps being brought up, most recent case was around a performance regression: https://cl4es.github.io/2019/02/21/Cljinit-Woes.html

So keep static fields for objects you know and won't be doing heavy computation like initializing empty collections or ones with few elements. Most of the time the initialization doesn't need to happen before the first use of said member, so better put it in some lazy-init container or create one. Perhaps Lazy Static Final Fields will bring something simpler to use.

9

u/[deleted] Mar 21 '21

Your reply is great.

3

u/sadfsdffsdafsdfsdf Mar 22 '21

There's nothing to fix as it's documented behavior.

While I believe you, could you however be so kind and point to that documentation? I would be really curious about that.

1

u/slaymaker1907 Mar 22 '21

I disagree that this should be seen as acceptable because while it would make sense for there to be performance problems, this seems like a correctness issue.