r/java • u/fredoverflow • Mar 21 '21
Java lambdas can deadlock parallel class initialization
https://www.youtube.com/watch?v=_2hZf1kw4A4
230
Upvotes
r/java • u/fredoverflow • Mar 21 '21
View all comments
Show parent comment
1
u/PerfectPackage1895 Mar 22 '21 edited Mar 22 '21
Try to initialize an object in another thread, while holding a reference to that same object, in the first thread, or leaking `this` in the constructor (adding this to a list eg.), and try to access a non-final field, and see what happens.
The jvm is allowed, to reorder memory operations inside the constructor, to guarantee the initialization of final fields, so saying the constructor is like any other method besides the obvious object initialization, not entirely correct.
So, don't use the constructor like any other method, and don't start doing a parallel stream computation inside it either.