简体   繁体   English

多个线程搜索数组的一部分

[英]multiple threads that search a section of an array

i am given the following code that makes a large array and finds that max value. 我得到以下代码,该代码使一个大数组并找到该最大值。

import java.util.Date;
import java.util.Random;

class FindMax {
private static final int N = 256 * 1024 * 1024;

public static void main(String args[]) {
    assert(N > 0);

    int array[] = new int [N];

    assert(array != null);

    Random random = new Random(new Date().getTime());

    for (int i = 0; i < N; i++) {
        array[i] = random.nextInt();
    }

    Date start = new Date();

    int max = array[0];

    for (int i = 1; i < N; i++) {
        if (array[i] > max) {
            max = array[i];
        }
    }

    Date end = new Date();

    System.out.println("max: " + max);
    System.out.println("time in msec: " + (end.getTime() - start.getTime()));
}
}

I am to alter the code to make it faster by making multiple threads that each find the max value of a section of the array and then the main thread find the max value of the max values found by the threads. 我将通过使多个线程每个都找到数组的某个部分的最大值,然后主线程找到这些线程所找到的最大值的最大值,来更改代码以使其更快。 This is what i have come up with so far. 到目前为止,这是我想出的。

import java.util.Date;
import java.util.Random;

class FindMax extends Thread{
private static final int N = 256 * 1024 * 1024;

static int array[] = new int [N];
static int max = array[0];

public void run(){
    for (int i = 1; i < N; i++) {
        if (array[i] > max) {
            max = array[i];
        }
    }

}

public static void main(String args[]) {
    assert(N > 0);
    int ts = Integer.parseInt(args[0]);


    assert(array != null);

    Random random = new Random(new Date().getTime());

    for (int i = 0; i < N; i++) {
        array[i] = random.nextInt();
    }

    Date start = new Date();

    Thread t[] = new Thread[ts];
    for( int p=0; p<ts;p++){
        t[p] = new FindMax();
        t[p].start();
    }





    Date end = new Date();

    System.out.println("max: " + max);
    System.out.println("time in msec: " + (end.getTime() - start.getTime()));
}
}

i am not understanding this, so what am i doing wrong? 我不明白这一点,所以我做错了什么?

You're off to a good start. 您的开端很好。 The next thing you need to do is give the FindMax class two member variables to represent the beginning and end of the range to search. 接下来需要做的是给FindMax类两个成员变量,以代表要搜索范围的开始和结束。 Use those two member variables in place of 1 and N in the for loop. for循环中使用这两个成员变量代替1N Then give FindMax a constructor with which you can set the two values. 然后给FindMax一个构造函数,您可以用它来设置两个值。 Then in the loop that constructs the FindMax objects, give each one a unique range to search. 然后在构造FindMax对象的循环中,为每个对象指定一个唯一的范围进行搜索。 Finally, give FindMax a member variable to store the max value in, so that main() can query the max value found by each FindMax . 最后,给FindMax一个成员变量来存储最大值,以便main()可以查询每个FindMax找到的FindMax

You'll probably want to use the join() method of the Thread class -- it waits for a Thread to finish before returning. 您可能要使用Thread类的join()方法-它等待Thread完成后再返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM