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

8

u/r0estir0bbe Mar 21 '21

Why not? That seems like a valid use case to me, e.g. for computing specific constants - not great, but not terrible?

6

u/[deleted] Mar 21 '21

If they are constant why are you computing them?

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.

There’s other ways to achieve a similar result.

5

u/john16384 Mar 21 '21

A constant can be a huge lookup table.

1

u/sweetno Mar 21 '21

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.