查找10个线程的最大值 [英] Find Max Value of 10 Threads

查看:89
本文介绍了查找10个线程的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a program that sorts though a text file and pulls out the maximum value using 10 threads. How can I then sort through the 10 threads and find the highest value of those 10? My logic would be to store each result in an array and compare that result to the previous, but I'm unsure on how to properly implement it with threading. I added this for loop but it's not correct.Any help would be greatly appreciated!

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

这是包含上面代码的实际程序.没有它,它运行正常.

This is the actual program including the code above. It runs fine without this.

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;
        }

    }
}

推荐答案

基本上,您的最大计算错误.这是更正的代码.

Basically your max calculation was wrong. Here is the corrected code.

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 );

这篇关于查找10个线程的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆