将代码转换为多线程版本

发布于 2024-11-04 10:25:42 字数 278 浏览 0 评论 0原文

我有以下问题:

  • 例如,我有 10 个列表,每个列表都有一个到其他列表的链接,我想创建一个代码来搜索这些列表中的元素,我已经完成了这个算法,但按顺序,它开始在第一个列表中搜索,如果搜索失败,它会发送消息以在与其有链接的列表中搜索(到第一个列表),在算法结束时,他将结果显示为列表的数量访问过,是否找到该元素。
  • 现在,我想将其转换为一种并行算法,至少是使用多线程的并发算法:
    • 使用线程进行搜索;
    • 同时在 10 个列表中开始搜索;

I have the following problem:

  • for example, I have 10 lists, each one has a link to some other lists, I would like to create a code to search elements in theses lists, I've already done this algorithm but sequentially, it start the search in the first list, then if the search is failed, it send messages for searching in lists that have a link with it (to the first one), at the end of the algorithm, he show the results as the number of lists visited and if he find the element or no.
  • now, I want to transform it to be a parallel algorithm, at least a concurrent one using multi-threads:
    • to use threads for searching;
    • to start a search in the 10 lists at the same time;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红焚 2024-11-11 10:25:42

只要您不更改任何内容,您就可以将您的搜索视为只读。在这种情况下,您可能不需要同步。如果您想要快速搜索,请不要直接使用线程,而是使用可运行对象并查找适当的类。如果您直接使用线程,请确保不超过处理器的数量。

在进一步讨论之前,请先阅读多线程。我会提到“Java 并发实践”作为(相当安全的)推荐。太容易出错了。

As long as you don't change anything, you can consider your search read only. In that case, you probably don't need synchronization. If you want to have a fast search, don't use threads directly but use runnables and look for the appropriate classes. If you do work directly with threads, make sure that you don't exceed the number of processors.

Before going further, read into multi-threading. I would mention "Java Concurrency in Practice" as a (rather safe) recommendation. It's too easy to get wrong.

两相知 2024-11-11 10:25:42

我从您的问题陈述中不确定不同列表中的搜索之间是否存在顺序依赖性,即第一个列表中的搜索结果是否应优先于第二个列表中的搜索结果。

假设不存在这种依赖性,因此任何列表中的任何搜索结果都很好,那么这在 Ateji PX 中以非常简洁的方式表示为推测性搜索:

Object parallelSearch(Collection<List> lists)
{
  // start of parallel block
  [
    // create one parallel branch for each of the lists
    || (List list: lists) {

       // start searching lists in parallel
       Object result = search(list);

       // the first branch that finds a result returns it, 
       // thereby stopping all remaining parallel branches
       if(result != null) return result;
    }
  ]
}

术语“推测性”意味着您并行运行多个搜索,尽管您知道您只会使用其中之一的结果。然后,一旦您在其中一个搜索中找到结果,您就想停止所有剩余的搜索。

如果您在 4 核计算机上运行此代码,Ateji PX 运行时将一次调度 4 个并行分支,以便充分利用可用的并行硬件。

I am not sure from your problem statement if there is a sequential dependency between the searches in different lists, namely whether search results from the first list should take priority over those from the second list or not.

Assume there's no such dependency, so that any search result from any list is fine, then this is expressed in a very concise way as a speculative search in Ateji PX:

Object parallelSearch(Collection<List> lists)
{
  // start of parallel block
  [
    // create one parallel branch for each of the lists
    || (List list: lists) {

       // start searching lists in parallel
       Object result = search(list);

       // the first branch that finds a result returns it, 
       // thereby stopping all remaining parallel branches
       if(result != null) return result;
    }
  ]
}

The term "speculative" means that you run a number of searches in parallel although you know that you will use the result of only one of them. Then as soon as you have found a result in one of the searches, you want to stop all the remaining searches.

If you run this code on a 4-core machine, the Ateji PX runtime will schedule 4 parallel branches at a time in order to make the best use of the available parallel hardware.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文