I don't know when I should use Multithread in Java development, and the logic/reason of using it. How does it help in different scenarios?
I'm making a 2D scrolling game with enemies and a all that, think of metal slug
I'm making a database daemon to check multiple database content.
I'm making a servlet for JSP, with some information fetched from the database.
Feel free to edit the scenarios to make it better.
In addition: Should I use multithread for game servers? Or should I not?
I don't know when I should use Multithread in Java development, and the logic/reason of using it. How does it help in different scenarios?
You should change your program to use threads for a couple of different reasons.
When the program would run significantly faster and make better use of the multiple CPU/core architecture that you are running on. I use the word "significantly" because often adding threads adds a lot of complexity so a 20% speed improvement may not be worth it.
It can be difficult, however, to determine if your program will properly make use of multiple processors so that the reworking of the program is a good investment. Only if there is a lot of processing/calculating involved will you get a speed increase. If, for example, your program is waiting for IO (disk or network reading or writing) then you might spend a lot of work splitting a program into multiple threads only to see no speed improvement.
When there are multiple parts of your program that should be running simultaneously. It's not that it couldn't run in one thread but to do so would be more complicated. For example, a web server has multiple request handling threads because it is easier to have each thread handle a single request even though you may not be putting a ton of load through the server so that multiple threads makes it perform faster.
In addition: The same question for J2EE stuff, when should I use multithread in my servlets? Or should I not?
I think the same answers above apply. In general servlets are very small tasks that are designed to return quickly so it is relatively unusual for them to fork threads. However, if there is a long-running task that a servlet needs to start but you want it to return a "Starting job" type of response then a thread will be necessary.
It is important to note that by the time your servlet is executed, the upstream handler is probably already making use a thread pool so you don't have to do anything.
Edit:
Scenario 1 - I'm making a 2D scrolling game with enemies and a all that, think of metal slug
I don't have a good answer for this. Depends on whether there is a lot of rendering going on and it depends on what toolkits you are using.
Scenario 2 - I'm making a database daemon to check multiple database content.
Chances are you are going to be database IO bound so multiple threads may not give you anything. Then again, if you have long running queries, you might get some improvement if short queries could be executing in parallel on other threads. This also depends on how your database handles multiple connections.
Scenario 3 - I'm making a servlet for JSP, with some information fetched from the database.
If the response has to wait for the information to be fetched then there is no reason to do this in another thread. However, as I mentioned above, if the servlet is trying to fork some sort of database transaction that runs in the background then you should use a thread.
Again, most servlet containers already are running inside a thread pool.
Let's say that you have an application that's in charge of downloading a file, if you don't use multiple threads, all other instructions would wait until the file finish to download. Swing for example use a special thread in order to display his gui, so you should take a look to this tutorial and read a little bit of concurrency
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With