问题
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?
Scenario 1
I'm making a 2D scrolling game with enemies and a all that, think of metal slug
Scenario 2
I'm making a database daemon to check multiple database content.
Scenario 3
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?
回答1:
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.
回答2:
Use Multi-Threading when you can think something can be parallelized or can be made asynchronous.
Parallelized For example: While building Web-crawlers, you can have parallel threads which browse through different pages etc. This might also mean to better utilize resources (CPU, I/O etc)
Asynchronous in the sense, in the case of produce-consumer where you have a block of code producing stuff and the rest consuming it.
回答3:
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
回答4:
I don't know when I should use Multithread in Java development
-Then you should not. Also, if you can avoid it, by all means do so.
When: An example: If you need to fetch market share values of different stocks from multiple sites/resources exactly at the same time to update different broker systems, you may need to as a single thread will have to get from one site first before it tries getting from the next one.
Based from your edit in your question-
Secnario 1: I'm making a 2D scrolling game with enemies and a all that, think of metal slug
Yes you can. An example would be do the loading of resources in one thread while displaying a "Loading..." animation on the screen in another thread.
回答5:
It's not really like you once learn multithreading, then you use it all the time everywhere. You use multithreading when you need it.
For example you have a long task to do, and you don't want to lock the UI, you can do the task on another thread while your UI still works well. That would be one scenario.
Another would be to calculate something complex that can be done in many independent steps, then it could be done in parallel using multithreading.
来源:https://stackoverflow.com/questions/18236597/when-why-should-i-use-multithread-in-java