如何编写函数以并行查找大于N的值

塔默耶尔

因此,我有一个函数,可以在下面显示的大型未排序数字数组中找到大于N的数字。

import java.util.*;

public class program {

    // Linear-search function to find the index of an element
    public static int findIndex(int arr[], int t)
    {
        // if array is Null
        if (arr == null) {
            return -1;
        }

        // find length of array
        int len = arr.length;
        int i = 0;

        // traverse in the array
        while (i < len) {

            // if the i-th element is t
            // then return the index
            if (arr[i] > t) {
                return i;
            }
            else {
                i = i + 1;
            }
        }
        return -1;
        }

   // Driver Code
   public static void main(String[] args)
   {
      int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };

      int i = findIndex(my_array, 7);
       // find the index of 5
       System.out.println("Index position of 5 is: "
                    + my_array[i]);
   }
}

但是我必须找到一种并行实现此方法的方法。我不确定如何开始或究竟要做什么,因为我在并行编程领域还很陌生。

任何帮助将不胜感激。

梦境崩溃

最直接的方法是使用@Govinda Sakhare很好地说明并行流

但是,如果您想使用此示例作为学习如何使用线程的方法,请按照以下步骤并行化代码:

  1. 创建线程;
  2. 将工作分配给线程,每个线程将尝试查找一个值,该值大于作为参数传递的值,但仅用于数组的一部分;
  3. 找到该值的第一个线程将其通知其他线程,以便每个线程退出。

要创建线程,我们可以执行以下操作:

Thread[] threads = new Thread[total_threads];
for(int t = 0; t < threads.length; t++) {
    threads[t] = new Thread(/** the parallel work **/);
    threads[t].start();
}

要将工作分配给线程,我们需要在线程之间拆分数组。最简单的方法实际上是拆分迭代而不是数组本身。线程接收整个数组,但是只能使用其某些位置,例如:

private final static int NO_FOUND = -1;

// Linear-search function to find the index of an element
public static int findIndex(int[] arr, int t, int threadId, int total_threads){
    for (int i = threadId; i < arr.length; i += total_threads)
        if ( arr[i] > t)
            return i;
    return NO_FOUND;
}

我们为每个线程分配一个ID从到范围0 to N-1N即线程总数。

为了通知线程,我们可以在线程之间使用共享的原子整数,这些原子将用于更新找到的值的索引。因此,最终代码如下所示:

public class program {
    private final static int NO_FOUND = -1;

    // Linear-search function to find the index of an element
    public static int findIndex(int[] arr, int t, int threadId, int total_threads, AtomicInteger shared_index){
        for (int i = threadId; i < arr.length && shared_index.get() == -1; i += total_threads)
            if ( arr[i] > t)
                return i;
        return NO_FOUND;
    }

    public static void main(String[] args) throws InterruptedException {
        final int N = 8;
        int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
        int total_threads = 4;

        AtomicInteger shared_index = new AtomicInteger(-1);

        Thread[] threads = new Thread[total_threads];
        for(int t = 0; t < threads.length; t++) {
            final int thread_id = t;
            threads[t] = new Thread(() ->parallel_work(N, my_array, total_threads, shared_index, thread_id));
            threads[t].start();
        }

        for (Thread thread : threads) 
            thread.join();
        
        System.out.println("Index of value bigger than " + N + " : " + shared_index.get());
    }

    private static void parallel_work(int n, int[] my_array, int total_threads, AtomicInteger shared_index, int thread_id) {
        int index_found = findIndex(my_array, n, thread_id, total_threads, shared_index);
        shared_index.compareAndExchange(NO_FOUND, index_found);
    }
}

输出:

Index of value bigger than 8 : 8

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何并行编写Java函数?

如何编写此递归函数以查找对象的最大深度?

从最近的日期回溯,如何查找大于n的连续值的数量

如何编写此迭代函数以递归?

如何编写自定义函数以显示数据框中每个变量的值计数以及级别?

编写R函数以查找二进制子集

如何编写递归函数以获取表示值的每个排列的集合的数组?

如何编写函数以指数方式将数字增加到最大值

单击按钮时如何编写js函数以获取输入值

如何编写通用函数以评估Spark数据框的列中的列值

编写 ar 函数以用以前的值替换缺失值

编写Haskell函数以计算函数的n次幂?

如何运行多个函数以并行返回各自的列表

如何在Powershell中编写函数以创建函数

Python:编写函数以检查列表并返回值或无

Haskell编写自己的函数以编写函数

如何编写函数以在调查中取平均值?

如何编写这些函数以使用forEach()语句?

如何编写pandas或dask函数以获取以下输出?

如何编写通用函数以在 C 中重用

如何编写扩展函数以在Kotlin中实例化AndroidViewModel?

如何编写Java函数以返回动态类型的数组?

如何编写函数以动态查询熊猫数据框?

DART:如何编写耗时的函数以返回Future

如何编写JavaScript函数以检查JRE版本

如何编写函数以在OCaml中创建列表的循环版本?

如何编写函数以获取树状结构的输出

如何编写Go函数以接受不同的结构?

如何编写constexpr交换函数以更改整数的字节序?