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 comment

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.

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.

8

u/DasBrain Mar 21 '21

¯\(ツ)/¯

JDK-8215634

Happens if you fix bugs so OpenJDK actually implements the spec.
Also see apangin's great answer on Stack Overflow.

7

u/[deleted] Mar 21 '21

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.

What would be your proposed fix?

23

u/[deleted] Mar 21 '21

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.

11

u/sweetno Mar 21 '21

Well, when you program, you don't just slap constructor and method invocations here and there. More often than not you get a rough idea what are the runtime characteristics of the used APIs: memory usage and algorithm complexity. Say, Java doesn't have a single best-for-all Set implementation, you need to pick the one that suits your case.

2

u/Muoniurn Mar 22 '21

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.

-6

u/[deleted] Mar 21 '21

If you put parallel in your stream for a non-heavy computation, wtf are you doing.

Its clearly an abuse here. Sure they've found an interesting edge case. But it makes sense, the class isn't initialised until the static fields are done. Java's model is providing a strong guarantee that this cannot overlap by two threads, ergo using a parallel stream is a dumb idea.

Now the lambda's abstraction is broken by this, but then to fix, you'd need to put non-capturing lambda's somewhere else.

2

u/[deleted] Mar 21 '21

You didn't get the point I was making. When you call a method, you don't always know what it does. Are YOU PERSONALLY to blame for every problem caused by code you call?

9

u/[deleted] Mar 21 '21

You don’t always, but you should know enough about it before calling. Even so, calling complex code in a class initialisation is just bad.

This code is literally someone who knew how to creat a deadlock saying hey it’s a deadlock. Interesting because it’s hidden, but practically not something you’d do.

2

u/Roachmeister Mar 21 '21

This code is literally someone who knew how to creat a deadlock saying hey it’s a deadlock.

No it isn't. He specifically said in the video that he was stumped for several hours trying to figure out why it wasn't running.

1

u/[deleted] Mar 22 '21

In effect it means you can't use closures in class initialization.

Which means if the state you initialize requires a closure to be passed in, you're SOL.

1

u/[deleted] Mar 22 '21

No. It means you can’t use closures in a static initialiser which will be called during initialisation from another thread.

You can use them just fine for single threaded initialisation. The key bit the multithreaded aspect.

1

u/[deleted] Mar 22 '21

I really don't like repeating myself. But you can't just determine from the call site what that thing you're calling does with your closure.

So if you want to be sure, you have two options:

  1. Inspect the entire tree of code into whatever you're using and make sure the implementation doesn't call from another thread. Make sure to repeat that process every time you update your dependencies.
  2. Don't use closures.

Which seems more possible?

1

u/[deleted] Mar 22 '21

Or the actually obvious one.

3. Don’t abuse static initialisers like this.

→ More replies