java multithread,什么时候/为什么我应该在Java中使用Multithread?

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值