第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用多線程查找數(shù)組中的 N 個(gè)最大元素

使用多線程查找數(shù)組中的 N 個(gè)最大元素

慕森卡 2022-05-21 20:38:27
我有一個(gè)簡單的問題:給定一個(gè)數(shù)字?jǐn)?shù)組,找到數(shù)組的 N 個(gè)最大數(shù)字,但我需要使用多線程來解決這個(gè)問題,比如使用 10 個(gè)線程。我不想對數(shù)組進(jìn)行排序:只需遍歷它,并將每個(gè)元素與大小為 N 的結(jié)果數(shù)組的最小值進(jìn)行比較(用 初始化Double.MIN_VALUE)。遍歷數(shù)組后,結(jié)果數(shù)組將包含我輸入數(shù)組的最大 N 個(gè)元素。對于多線程,我不希望每個(gè)線程都有一個(gè)結(jié)果數(shù)組,這樣我以后就不必合并它們。這就是為什么我希望所有線程都對共享結(jié)果數(shù)組進(jìn)行操作。我意識到這不是最好的解決方案,但我仍然想了解我應(yīng)該如何實(shí)現(xiàn)它。我試過這個(gè),但它不起作用:public class Problem {private static final int MY_THREADS = 10;public static void main(String[] args) {    double[] array = {...};    double[] maximums = new double[3];    for (int i = 0; i < maximums.length; ++i) {        maximums[i] = Double.MIN_VALUE;    }    ExecutorService executor = Executors.newFixedThreadPool(MY_THREADS);    Runnable worker = new MyRunnable(array, maximums);    executor.execute(worker);    executor.shutdown();    while (!executor.isTerminated()) {    }    System.out.println(Arrays.toString(maximums));}public static class MyRunnable implements Runnable {    private double[] array;    private double[] maximums;    MyRunnable(double[] array, double[] maximums) {        this.array = array;        this.maximums = maximums;    }    @Override    public void run() {        int i = 0;        while (i < array.length) {            if (array[i] > maximums[getMinIndex(maximums)]) {                maximums[getMinIndex(maximums)] = array[i];            }            ++i;        }    }}private static int getMinIndex(double[] array) {    int minIndex = -1;    double min = Double.MAX_VALUE;    for (int i = 0; i < array.length; ++i) {        if (array[i] < min) {            min = array[i];            minIndex = i;        }    }    return minIndex;}}有人可以幫忙嗎?謝謝。
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

我不知道你為什么要多線程這樣的東西,然后還要避免排序 - 但是suuuuuuureeeee。你可以這樣做:


class Problem {

    private static final int MY_THREADS = 10;

    private static final double[] maximums = new double[3];



    public static void main(String[] args) {

        double[] array = {...};


        for ( int i = 0; i < maximums.length; ++i) {

            maximums[i] = Double.MIN_VALUE; //Remember that this won't work with negative values in array

        }


        ExecutorService executor = Executors.newFixedThreadPool(MY_THREADS);


        int start = 0;

        int length = array.length/MY_THREADS;

        for( int i = 0; i < MY_THREADS; i++ )

        {

            //You probably want to give it only part of array to consider,

            //otherwise you are wasting resources and might even try to insert same element more than once.

            Runnable worker = new MyRunnable(Arrays.copyOfRange( array, start, start + length ) );

            executor.execute(worker);

            start += length;

        }


        executor.shutdown();


        while (!executor.isTerminated()) {


        }

        System.out.println( Arrays.toString( maximums ));

    }


    //This is unsynchronized - but with this problem - it will at worst cause unnecessary insert attempt.

    private static int getMinIndex() {

        int minIndex = -1;

        double min = Double.MAX_VALUE;


        for (int i = 0; i < maximums.length; ++i) {

            if (maximums[i] < min) {

                min = maximums[i];

                minIndex = i;

            }

        }

        return minIndex;

    }


    //You have to synchronize the insertion somehow,

    // otherwise you might insert two values into same spot losing one of max.

    private static synchronized void insertIntoMaximum( double k ){

        int minIndex = getMinIndex();

        if( maximums[minIndex] < k ){

            maximums[minIndex] = k;

        }

    }


    public static class MyRunnable implements Runnable {

        private double[] array;


        //Since its inner class, I didn't think passing maximums into it was necessary.

        // You could pass it here, but you would still probably need to call parent for insertion.

        MyRunnable(double[] array) {

            this.array = array;

        }


        @Override

        public void run() {


            //this while was an interesting attempt at making indexed for, that doesn't even need to be indexed.

            for( double v : array )

            {

                if( v > maximums[getMinIndex()] )

                {

                    insertIntoMaximum( v );

                }

            }

        }

    }

}

我仍然可能會(huì)避免使用多線程,創(chuàng)建一個(gè)新線程可能非常昂貴 - 所以它很可能甚至不會(huì)節(jié)省時(shí)間,特別是考慮到您仍然需要同步插入。


查看完整回答
反對 回復(fù) 2022-05-21
  • 1 回答
  • 0 關(guān)注
  • 208 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號