什么时候/为什么我应该在Java中使用Multithread? [英] When/Why should I use Multithread in Java?

查看:148
本文介绍了什么时候/为什么我应该在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?

我正在与敌人进行2D滚动游戏,想想金属slug

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.

我正在为JSP创建一个servlet,并从数据库中获取一些信息。

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?

推荐答案


我不知道何时应该在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?

您应该将程序更改为使用线程,原因有两个。

You should change your program to use threads for a couple of different reasons.


  • 当程序运行速度明显加快并更好地利用您运行的多CPU /核心架构时。我使用显着这个词,因为经常添加线程会增加很多复杂性,因此20%的速度提升可能不值得。

  • 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.

但是,确定您的程序是否能正确使用多个处理器可能很困难,因此重新编程是一项很好的投资。只有在涉及大量处理/计算时才会提高速度。例如,如果您的程序正在等待IO(磁盘或网络读取或写入),那么您可能会花费大量工作将程序拆分为多个线程,只是为了看不到速度提升。

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.

当程序的多个部分应该同时运行时。这不是它不能在一个线程中运行,但这样做会更复杂。例如,Web服务器有多个请求处理线程,因为每个线程处理单个请求更容易,即使您可能没有通过服务器放置大量负载,以便多个线程使其执行得更快。

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.


此外:对于J2EE的问题,我何时应该在servlet中使用多线程?或者我不应该?

In addition: The same question for J2EE stuff, when should I use multithread in my servlets? Or should I not?

我认为上述相同的答案适用。通常,servlet是非常小的任务,旨在快速返回,因此它们分叉线程相对不常见。但是,如果有一个长时间运行的任务,servlet需要启动但你希望它返回一个Starting job类型的响应,那么就需要一个线程。

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.

值得注意的是,在执行servlet时,上游处理程序可能已经在使用线程池,因此您无需执行任何操作。

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.

编辑:


场景1 - 我正在与敌人进行2D滚动游戏还有一点,想想金属slu

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.


场景2 - 我正在制作用于检查多个数据库内容的数据库守护程序。

Scenario 2 - I'm making a database daemon to check multiple database content.

您可能会受到数据库IO限制,因此多个线程可能无法为您提供任何内容。然后,如果您有长时间运行的查询,如果短查询可以在其他线程上并行执行,您可能会得到一些改进。这还取决于数据库如何处理多个连接。

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.


场景3 - 我正在为JSP创建一个servlet,其中包含一些信息数据库。

Scenario 3 - I'm making a servlet for JSP, with some information fetched from the database.

如果响应必须等待获取信息,那么没有理由在另一个线程中执行此操作。但是,正如我上面提到的,如果servlet正在尝试派生某种在后台运行的数据库事务,那么你应该使用一个线程。

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.

同样,大多数servlet容器已经在线程池中运行。

Again, most servlet containers already are running inside a thread pool.

这篇关于什么时候/为什么我应该在Java中使用Multithread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆