Without knowing more about the context, it is difficult to give a good answer, but, generally speaking, I would say that situations that require the use of Thread are quite small and far from each other. If you start trying to synchronize your program “manually” using synchronized , I’m sure that everything will quickly get out of hand. (Not to mention how difficult it will be to debug the code.)
Last time I used the stream when I wanted to record some kind of sound in the background. It was a start / stop, not a task-oriented one. (I tried my best to try and find an audio library that encapsulated this for me, but failed.)
If you decide to go for a streaming solution, I suggest that you try to limit the scope of the stream to only be executed on the associated object. This will, as far as possible, not make you think about what happened - before a relationship, thread-safe publication of values, etc. In the whole code.
source share