Java,数组,线程数,最小值,最大值

我一直在尝试编写一个程序来查找数组中的最大值和最小值。 我做了第一部分。该程序将Array分为两部分,分别显示每半部分的min和max,然后显示整体的min和max。 我的第二部分有问题-我不知道如何更改此程序,以便您可以提供任意数量的线程。

请查看我的代码:

{
    smallThread first;
    smallThread second;

    public Main(String fName, String sName)
    {
        first = new smallThread(fName);
        second = new smallThread(sName);
    }

    public static void main(String[] args)
    {
        Main main = new Main("First", "Second");
        main.startThread();
        main.waitForIt();
        main.showItBaby();
    }

    private void startThread() {
        first.start();
        second.start();
    }

    private void waitForIt() {
        while (first.isAlive() || second.isAlive()) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void showItBaby() {
        int max = smallThread.send(0);
        int min = smallThread.send(0);
        for (int i = 0; i < 20; ++i) {
            int element = smallThread.send(i);
            if (element > max) { max = element; }
            if (element < min) { min = element; }
            //System.out.println(element);
        }
        System.out.println("Min: " + min);
        System.out.println("Max: " + max);
    }
}









import java.util.Random;

public class smallThread extends Thread
{
    private int[] tab = new int[200];
    private static int[] tabX = new int[200];
    private int xNumber;
    private int yNumber;
    private int maxi;
    private int mini;
    private int aNumer = 0;

    public smallThread(String str) { super(str); }

    public synchronized void run() {

        long millisActualTime = System.currentTimeMillis();
        Random x = new Random();
        for (int i = 0; i < 20; i++) { tab[i] = x.nextInt(100); }

        if (getName().equals("First")) {
            xNumber = 0;
            yNumber = 10;
            mini = tab[0];
            maxi = tab[0];
            for (int i = xNumber; i < yNumber; i++)
            {
                if (tab[i] > maxi) { maxi = tab[i]; }
                if (tab[i] < mini) { mini = tab[i]; }
            }
            System.out.println("Min 1 thread: "+mini);
            System.out.println("Max 1 thread: "+maxi);
            adding();
        }
        if (getName().equals("Second")) {
            xNumber = 10;
            yNumber = 20;
            mini = tab[10];
            maxi = tab[10];
            for (int i = xNumber; i < yNumber; i++)
            {
                if (tab[i] > maxi) { maxi = tab[i]; }
                if (tab[i] < mini) { mini = tab[i]; }
            }
            System.out.println("Min 2 thread: "+mini);
            System.out.println("Max 2 thread: "+maxi);

            long executionTime = System.currentTimeMillis() - millisActualTime;
            System.out.println("Czas wykonania: "+executionTime);

            adding();
        }
    }

    private void adding() {
        for (int i = xNumber; i < yNumber; ++i) {
            tabX[i] = tab[i];
        }
    }

    public static int send(int a)
    {
        return tabX[a];
    }

}
评论