在 Java 中使用线程进行集群
我有一份 Java 工作需要很长时间。所以我想把这个工作分成线程并运行它们。线程完成其工作后,返回到我的服务,并且服务为它们提供新的工作。 ThreadGroup 适合这个还是有其他推荐?
I have a job that takes too long time in Java. So I want to divide this job into threads and run them. After the threads finishes their jobs, returns to my service and Service give them new jobs. ThreadGroup is suitable for this or any other recommendation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,如果满足以下条件,则需要线程:
a) 您有一台多处理器计算机
,或者 b) 您有一个处理器,但您的作业是 IO 密集型(而不是 CPU 密集型)
,否则,使用线程时您将一无所获。
您需要的是 ThreadPool
First of all, you need threads if either:
a) You have a multiprocessor machine
or b) You have a single processor but your jobs are IO-intensive (and not CPU-intensive)
Otherwise, you will gain nothing when using threads.
What you need here is ThreadPool
查看 ExecutorCompletionService - 它正是这样做的。
示例:[摘自 Java 6 API JavaDocs]
Check out the ExecutorCompletionService - it does exactly this.
Example: [pulled from Java 6 API JavaDocs]
不确定您的项目当前处于什么开发状态,因为您的问题陈述非常有限,但您可能需要考虑查看 JDK7 中的 fork-join 项目: http://www.ibm.com/developerworks/java/library/j-jtp11137.html
有收获很多通过查看它来学习,并且由于它都是开源的,因此您已经可以下载代码作为补丁并尝试使用它。
(可能不适用于您现在必须实现的任何内容,但如果您打算在未来一段时间内开发/维护您的应用程序,仍然值得一看)
Not sure in what state of development your project currently is, since your problem statement is quite limited, but you might want to consider getting having a look at the fork-join project coming in JDK7: http://www.ibm.com/developerworks/java/library/j-jtp11137.html
There's a lot to gain & learn from looking at that, and since it's all open source you can already download the code as a patch and have a go at working with it.
(Might not be applicable for anything you have to implement right now, but worth a look non the less if you intend to develop / maintain your application for some time in the future)
看一下 java .util.concurrent 包。
有一个教程,您应该在这里找到您需要了解的所有内容:
http:// /java.sun.com/docs/books/tutorial/essential/concurrency/
重点关注 高级并发对象 特别是。
Take a look at the java.util.concurrent package.
There's a tutorial where you should find everything you need to know here:
http://java.sun.com/docs/books/tutorial/essential/concurrency/
Focus on the High Level Concurrency Objects in particular.
ThreadGroup
通常对应用程序代码没有多大用处。它对容器代码也没有多大用处。 Java PlugIn 使用ThreadGroup
来区分线程属于哪个applet。java.util.concurrent
,特别是ExecutorService
,提供了处理线程和并发的便捷实用程序。对于计算密集型细粒度任务,JDK7 中的 fork-join 框架将很有用。
在开始编写这段困难的代码之前,您可能需要考虑它是否值得。您可以进行其他不需要大规模线程使用的优化吗?您正在尝试处理 I/O 延迟吗?如果它是 CPU 密集型的,那么使用比硬件多得多的线程并没有多大意义。
ThreadGroup
isn't generally of much use to application code. It's not a great deal of use to container code either. The Java PlugIn usesThreadGroup
to distinguish which applet a thread belongs to.java.util.concurrent
, in particularExecutorService
, provides amongst other things handy utilities for handling threads and concurrency.For computationally intensive fine-grained tasks, the fork-join framework in JDK7 will be useful.
Before starting on this difficult code, you might want to consider whether it is worth it. Can you do other optimisations that doesn't require large scale thread use? Is it I/O latency you are trying to deal with? If it is CPU-intensive, there is not a great deal of point in using many more threads than you have in hardware.