並列選択ソートアルゴリズムのjava実装


本人は最近、通常のシリアル選択ソートアルゴリズムをパラレルアルゴリズムに変更し、マルチコアプロセッサでテストされた速度は、通常のシリアル選択ソートアルゴリズムの約1.5倍から2倍だった.ネット上の並列選択ソートアルゴリズムに関する紹介はほとんど見られなかったので、参考にしてみましょう.コードは以下の通りです.
import java.math.BigInteger;

import static java.util.concurrent.locks.LockSupport.park;
import static java.util.concurrent.locks.LockSupport.unpark;

class ComValues
{
    private BigInteger arr[];
    private int nElem;
    private Thread mainThread;

    ComValues (BigInteger arr[], int num, Thread mainThread)
    {
        this.arr = arr;
        this.nElem = num;
        this.mainThread = mainThread;
    }

    public BigInteger[] getArr ()
    {
        return arr;
    }

    public void setArr (BigInteger[] arr)
    {
        this.arr = arr;
    }

    public int getnElem ()
    {
        return nElem;
    }

    public void setnElem (int nElem)
    {
        this.nElem = nElem;
    }

    public Thread getMainThread ()
    {
        return mainThread;
    }

    public void setMainThread (Thread mainThread)
    {
        this.mainThread = mainThread;
    }

}

class SelectSortRightThread extends Thread
{
    Thread thrd;
    private ComValues cvs;
    int max, left;
    int out, in;

    SelectSortRightThread (String name, ComValues cvs)
    {
        this.cvs = cvs;
        thrd = new Thread(this, name);
        thrd.start();
    }

    // This is the entry point for thread.
    public void run ()
    {
        int nElems = cvs.getnElem();
        BigInteger[] a = cvs.getArr();

        try
        {
            left = 0;
            out = nElems - 1;
            do
            {
                max = out; // maxima
                for (in = out - 1; in >= left; in--) // inner loop
                    if (a[in].compareTo(a[max]) > 0) // if max smaller,
                        max = in; // we have a new max
                left++;
                unpark(cvs.getMainThread());
                park();

                out--;
            } while (out >= (nElems % 2 == 0 ? nElems / 2 : nElems / 2 + 1)); // outer loop
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

class ArraySel
{
    private BigInteger[] a; // ref to array a
    private int nElems; // number of data items
    // --------------------------------------------------------------

    public ArraySel (int max) // constructor
    {
        a = new BigInteger[max]; // create the array
        nElems = 0; // no items yet
    }

    // --------------------------------------------------------------
    public void insert (BigInteger value) // put element into array
    {
        a[nElems] = value; // insert it
        nElems++; // increment size
    }

    // --------------------------------------------------------------
    public void display () // displays array contents
    {
        for (int j = 0; j < nElems; j++) // for each element,
            System.out.print(a[j].longValue() + " "); // display it
        System.out.println("");
    }

    // --------------------------------------------------------------
    public void selectionSort ()
    {
        int out, in, min, max, outTh;
        int right = nElems - 1;

        ComValues cvs = new ComValues(a, nElems, Thread.currentThread());
        SelectSortRightThread SSRT = new SelectSortRightThread("ssrt", cvs);

        for (out = 0; out <= nElems / 2 - 1; out++) // outer loop
        {
            min = out; // minimum
            for (in = out + 1; in <= right; in++) // inner loop
                if (a[in].compareTo(a[min]) < 0) // if min greater,
                    min = in; // we have a new min

            park();
            max = SSRT.max;
            outTh = SSRT.out;

            swap(out, min);
            if (!(max == out && outTh == min))
            {
                if (max == out)
                    swap(outTh, min);
                else
                    swap(outTh, max);
            }
            unpark(SSRT.thrd);
            right--;
        } // end for(out)
        //        service.shutdown();
    } // end selectionSort()
    // --------------------------------------------------------------

    private void swap (int one, int two)
    {
        BigInteger temp = a[one];
        a[one] = a[two];
        a[two] = temp;
    }
    // --------------------------------------------------------------
} // end class ArraySel
////////////////////////////////////////////////////////////////

class SelectSortApp
{
    public static void main (String[] args)
    {
        int maxSize = 5000; // array size
        ArraySel arr; // reference to array
        arr = new ArraySel(maxSize); // create the array

        for (int index = 0; index < maxSize; index++)
        {
            long n = (long) (java.lang.Math.random() * (maxSize - 1));
            arr.insert(new BigInteger(Long.toString(n)));               // insert items
        }

        arr.display(); // display items

        arr.selectionSort(); // selection-sort them

        arr.display(); // display them again
    } // end main()
} // end class SelectSortApp
////////////////////////////////////////////////////////////////