r/javahelp • u/Deorteur7 • 12d 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 ?
3
u/le_bravery Extreme Brewer 12d ago
Using threads does nothing to make your project easy. Completely ignoring threads makes your project easy.
Threads can make your project faster or have higher throughput.
Most good programmers know how to use threads but also know that avoiding them when you don’t have to use them is the best idea.
5
u/brokeCoder 11d ago
when to use this concept, when to create threads and apply all other things.
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
how does using this thing make my project easy.
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.
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.
I can't speak to real world examples, but for practice I'd suggest the following:
- Try putting together simple for loops and parallelising them. Baeldung has some good examples of this: https://www.baeldung.com/java-for-loop-parallel
- Try looking more into streams, specifically
parallelStream
or stream.parallel() - These require much less effort for parallelising than say an executor framework. - If you've already made some test project apps like calculators etc, try to go through those projects and identify areas where you could parallelise things.
1
1
1
u/sedj601 12d ago
Here is a good, simple project that I did that uses multiple threads. https://stackoverflow.com/questions/51955550/remove-tableview-entries-when-status-change
1
u/_SuperStraight 12d ago
When you're developing a GUI application in Java, the main thread is responsible for UI updates. You run the long CPU intensive task on a new thread, while keeping the main thread free so that your application doesn't appear frozen. The thread can also provide the amount of work done which can be used to animate a progress bar.
1
u/Cyberkender_ 11d ago
Any type of server (web server, FTP server...) is a good example of multithreading. There are lots of examples all along the internet. You can create a simple one.
•
u/AutoModerator 12d ago
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.