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.
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.
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.
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.
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.
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.
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.
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?
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.
Even if you need to compute them once don’t do it in an initialiser like that. This is why streams can be bad, a simple line initialisation is actually a bigger computation; and just because you can write it on a line doesn’t mean you should.
You asked why a constant may require calculation. I gave an example where this can be the case. Just because a constant needs sone calculation doesn't make it any less a constant.
There are examples in compression and encryption where these constants can't even be pre-computed and loaded from say a database, but we don't even have to look further than Java's Pattern#compile method for constants that are almost always static and require some significant calculation.
I am not saying though that multiple threads should be allowed, but I can't help but wonder how much stuff would break if Pattern#compile did this.
Your mixing terminology though. A physical constant is often only calculated once, because it never changes. Sounds like your taking about invariants, which are constant over a certain portion of calculations.
Really though, you shouldn’t do such things in a static initialiser, because that’s not where large calculations should go, and they are part of a class initialisation phase lower than user level.
I wonder if the obsession with immutable classes is why many don’t see an issue with this.
Huge lookup tables with constants are better be read from a file/DB. If reading/computing them is long, the program should ensure feedback for the user: logging info messages/displaying them in the GUI. Users don't like when programs consume system resources without visible effects. Static initializers are not good places to implement this sort of feedback.
The issue is that when you use the old way of Thread and Runnable you get a inner class. The lambda makes a static inner class. Different byte code - so to the lesson I guess is don't assume these are equivalent - even though IDEs usually give you that option to convert from one form to the other.
I think the lesson is to not do something weird with a class initialiser like that. Basically this is saying please refer to me before I’m in initialised.
An inner class would be illegal since it’s a static context.
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.