多线程归并排序
有人可以给我一个链接或为我提供多线程合并排序的java代码吗?
最好使用执行器!
非常感谢!
Can someone please give me a link or provide me with java code of a multithreaded merge sort?
Preferable one that uses an executor!
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我使用 2 个线程的 MergeSort 版本,它可以轻松扩展到 n 个线程(只需将原始数组拆分为 n 个子数组)。对于 1000 万个数字,它比单线程的速度快约 25%。
Here is my version of MergeSort using 2 threads, it can be extended to n threads easily (just split the original array into n sub-arrays). For 10 million numbers, it is faster than its single-thread counterpart by about 25%.
我建议多线程合并排序与常规合并排序非常相似,但是,当递归调用列表的每一半的合并排序时,请设置算法以在新线程中调用每个合并。然后,您可以等到两个线程都完成,然后将两个列表合并在一起,然后返回。简单的!
您在问题中说您想使用
Executor
- 我建议java.util.concurrent
包可用于此目的,特别是未来
界面。资源:
I'd suggest that a multithreaded mergesort is very similar to a regular mergesort, however, when recursively calling the mergesort each half of the list, set up your algorithm to call each merge in a new Thread. You can then wait until both threads are finished, and then merge the two lists together, and return. Simple!
You say in your question that you want to use an
Executor
- I'd suggest that thejava.util.concurrent
package would be of use for this, particularly theFuture
interface.Resources:
我尝试只使用 join 方法。它基本上为每个子问题生成线程,并使用 join 等待生成的线程完成。
让我知道您的意见。
I tried just using join method. It basically spawns the threads for each sub problem and waits for the spawned thread to complete using join.
Let me know your comments.
下面是一个使用 Java7 ForkJoin 框架的示例:
http://www.ibm.com/developerworks/java/library/ j-jtp03048.html
您可以通过使用此处的测试版本在 Java6 中使用此框架:
http://gee.cs.oswego.edu/dl/concurrency-interest/< /a>
Here is an example that uses the Java7 ForkJoin framework:
http://www.ibm.com/developerworks/java/library/j-jtp03048.html
You can use this framework in Java6 by using the beta version here:
http://gee.cs.oswego.edu/dl/concurrency-interest/