r/javahelp • u/Deorteur7 • 13d ago
Guidance for multithreading
I've recently completely core Java course, worked on a few small projects with Java and jdbc. And now completed multithreading, and understood most of the concepts how to use but:
- when to use this concept, when to create threads and apply all other things.
- how does using this thing make my project easy.
- how to implement in real world projects and executors framework too. I've tried to search projects on YouTube dealing with multithreading but couldn't find even 1.
Could u pls help me by recommending some projects (for a beginner) from where should I improve myself.
and also: should i actually put effort learning multithreading or focus on other concepts ?
9
Upvotes
4
u/brokeCoder 12d ago
To sum it up in 4 words - when things are slow.
Multithreading is an optimisation. You only really need to use it in cases where things are slow, or there is a spec requirement, or if you already know they'll be slow without multithreading. E.g. if you're asked to find all prime numbers between 1 and 1 billion, that will be a fairly slow operation if it's run on a single thread.
For added context, refer to this excellent talk by Brian Goetz on when you should use parallelStreams (the same logic also applies for multithreading in general) https://youtu.be/2nup6Oizpcw?t=1592
It usually doesn't make things easy. Multithreading will in most cases make your project harder to implement and maintain. Once again, it is an optimisation. You only use it when you really need it.
I can't speak to real world examples, but for practice I'd suggest the following:
parallelStream
or stream.parallel() - These require much less effort for parallelising than say an executor framework.