查找10个主题的最大值

时间:2015-11-01 22:34:22

标签: java arrays multithreading algorithm logic

我有一个程序可以对文本文件进行排序,并使用10个线程拉出最大值。然后,我如何对10个线程进行排序并找到这10个线程中的最高值?我的逻辑是将每个结果存储在一个数组中,并将结果与​​之前的结果进行比较,但我不确定如何使用线程正确实现它。我添加了这个循环,但它不正确。任何帮助将不胜感激!

 for (int x = 0; max <=max; x++) {
                max = worker.getMax();
                System.out.println("Final Max " = max);
            }

这是包含上述代码的实际程序。没有这个,它运行良好。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class datafile{

    public static void main(String[] args) throws IOException {
        int[] array = new int[100000];
        int count;
        int index = 0;
        String datafile = "dataset529.txt"; //string which contains datafile
        String line; //current line of text file

        try (BufferedReader br = new BufferedReader(new FileReader(datafile))) { //reads in the datafile
            while ((line = br.readLine()) != null) { //reads through each line
                array[index++] = Integer.parseInt(line); //pulls out the number of each line and puts it in numbers[]
            }
        }



        Thread[] threads = new Thread[10];
        worker[] workers = new worker[10];


        int range = array.length / 10;
        for (count = 0; count < 10; count++) {
            int startAt = count * range;
            int endAt = startAt + range;
            workers[count] = new worker(startAt, endAt, array);

        }

        for (count = 0; count < 10; count++) {
            threads[count] = new Thread(workers[count]);
            threads[count].start();
        }

        boolean isProcessing = false;
        do {
            isProcessing = false;
            for (Thread t : threads) {
                if (t.isAlive()) {
                    isProcessing = true;
                    break;
                }
            }
        } while (isProcessing);

        for (worker worker : workers) {
            System.out.println("Max = " + worker.getMax());
        }

        for (int x = 0; max <=max; x++) {
            max = worker.getMax();
            System.out.println("Final Max " = max);
        }

    }


    public static class worker implements Runnable {

        private int startAt;
        private int endAt;
        private int randomNumbers[];

        int max = Integer.MIN_VALUE;

        public worker(int startAt, int endAt, int[] randomNumbers) {
            this.startAt = startAt;
            this.endAt = endAt;
            this.randomNumbers = randomNumbers;
        }

        @Override
        public void run() {
            for (int index = startAt; index < endAt; index++) {

                if (randomNumbers != null && randomNumbers[index] > max)
                    max = randomNumbers[index];
            }
        }

        public int getMax() {
            return max;
        }

    }
}

1 个答案:

答案 0 :(得分:1)

基本上你的最大计算是错误的。这是更正后的代码。

int finalMax = workers[0].getMax(); //Sets max as first worker's max

for (int x = 1; x < workers.length; x++) {
     if(finalMax < workers[x].getMax())//checks whether finalMax is less than worker's max at x'th position and if yes assigns it to finalMax         
        finalMax = workers[x].getMax();        
}

System.out.println("Final Max " + finalMax );