r/java • u/fredoverflow • Mar 21 '21
Java lambdas can deadlock parallel class initialization
https://www.youtube.com/watch?v=_2hZf1kw4A422
u/sweetno Mar 21 '21
Putting anything non-trivial into static initialization is asking for a trouble.
3
u/dpash Mar 22 '21
I generally dislike them. Doubly so if it's the old collection hack to reduce verbosity.
A lazy evaluation wrapper is nicer.
8
u/Recolance Mar 21 '21
Interesting video. I always thought that lambda in this context was just fancy syntax magic.
Thanks for sharing.
2
15
u/Godworrior Mar 21 '21 edited Mar 21 '21
Another way to gain some insights in these situations is to use jcmd + jstack (both JDK tools) to see what the thread is getting stuck on:
"ForkJoinPool.commonPool-worker-15" #20 ... in Object.wait() [0x0000005e1a4fd000]
java.lang.Thread.State: RUNNABLE
at X$$Lambda$206/0x0000000800c23240.applyAsLong(Unknown Source)
- waiting on the Class initialization monitor for X
...3
u/PerfectPackage1895 Mar 21 '21
This is hardly any better than doing a heavy amount of calculation in the constructor, which also will end bad.
5
u/humoroushaxor Mar 22 '21
While I agree it's generally bad practice, there is no technical reason it is an issue. In the static initializer case there is, because it is part of a larger process out of your control.
I've seen plenty of code that does a lot of heavy lifting in the constructor that works fine.
2
u/PerfectPackage1895 Mar 22 '21 edited Mar 22 '21
There is no guarantee that the construction of the object has finished. So if you do this, you end in all kinds of trouble. If you work with multithreded applications you especially have to be very careful about constructors and object references with non final fields
1
u/humoroushaxor Mar 22 '21
There is no guarantee that the construction of the object has finished.
Constructors work the same way methods do, they can just only be called once per object and must be called first. Not sure what you're on about.
It sounds like your describing a constructor that assigns fields asynchronously which I agree would be terrible. But same with any method that returns an object still being initialized in another thread.
1
u/PerfectPackage1895 Mar 22 '21 edited Mar 22 '21
Constructors work the same way methods do, they can just only be called once per object and must be called first. Not sure what you're on about.
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.
1
u/ZeroGainZ Mar 24 '21
Wow, great video. I had no idea lambdas were static methods, I assumed they're syntactic sugar for creating the desired class. Also, I see some other redditors are saying "why would you do that", which may be true, but it's not at all obvious why it wouldn't work. It seems it should be a compiler error.
84
u/fredoverflow Mar 21 '21
TL;DW Lambda bodies are implemented as private static methods, but during class initialization, other threads are blocked from calling static methods.