john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Concurrency Multithreaded Simple

//2012-11-26 johnpfeiffer
package net.kittyandbear;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class Simple
{
    private int arraySize = 400000;
    private int bucketCount = 20;       //thread count matches bucket count
    int bucketIdOfMinimum = 0;
    int indexOfMinimum = 0;
    int currentMinimum;

    List<Thread> threads;

    public static void main(String args[]) throws InterruptedException
    {
        Simple main = new Simple();

        System.out.println("data = " + main.arraySize + " random integers in each array, " + main.bucketCount + " arrays in the List");
        List<int[]> dataBuckets = new ArrayList<int[]>();

        for (int i = 0; i < main.bucketCount; i++)
        {
            dataBuckets.add(main.setupRandomData(main.arraySize));
        }
//      main.displayRandomData( dataBuckets ); //DEBUG

        System.out.println("\nstart non threaded search for the index of the minimum value...");
        long start = System.currentTimeMillis();

        main.nonThreadedSearch(dataBuckets);
        long nonThreadedElapsed = System.currentTimeMillis() - start;
        System.out.println("NonThreaded = " + nonThreadedElapsed + " ms");
        System.out.println("NonThreaded Minimum " + dataBuckets.get(main.bucketIdOfMinimum)[main.indexOfMinimum] + " found at "
                + main.bucketIdOfMinimum + "," + main.indexOfMinimum);


        System.out.println("\nstart threaded search for the index of the minimum value...");
        start = System.currentTimeMillis();
        int threadResults[] = new int[main.bucketCount];
        main.threadedSearch(dataBuckets, threadResults);
        if (main.threads == null)
        {
            System.err.println("ERROR: unable to seed worker threads with jobs");
            System.exit(1);
        }

        int threadedBucketIndexOfMinimum = 0;
        int threadedIndexOfMinimum = 0;
        int threadedMinimum = dataBuckets.get(0)[0 ];

        for( int i=0; i<threadResults.length; i++ )
        {
            int bucketIndexOfMinimumValue = threadResults[i];
            int bucketMinimumValue = dataBuckets.get(i)[bucketIndexOfMinimumValue ];
            if( bucketMinimumValue <  threadedMinimum )
            {
                threadedBucketIndexOfMinimum = i;
                threadedIndexOfMinimum = bucketIndexOfMinimumValue;
                threadedMinimum = bucketMinimumValue;
            }
            System.out.println( "index = " + bucketIndexOfMinimumValue + " , value = " + bucketMinimumValue );
        }

        long threadedElapsed = System.currentTimeMillis() - start;
        System.out.println("Threaded = " + threadedElapsed + " ms");
        System.out.println("Threaded Minimum " + dataBuckets.get(threadedBucketIndexOfMinimum)[threadedIndexOfMinimum] + " found at " +
                threadedBucketIndexOfMinimum + "," + threadedIndexOfMinimum);

    }

    int[] setupRandomData(int size)
    {
        int array[] = new int[size];
        for (int i = 0; i < size; i++)
        {
            SecureRandom r = new SecureRandom();
            array[i] = r.nextInt();
        }
        return array;
    }

    void displayRandomData(List<int[]> dataBuckets)
    {
        int i = 0;
        for (int current[] : dataBuckets)
        {
            System.out.println("\n\nBucket # " + i);
            for (int k = 0; k < current.length; k++)
            {
                System.out.println(current[k]);
            }
            i++;
        }
    }

    void nonThreadedSearch(List<int[]> dataBuckets)
    {
        int currentMinimum = dataBuckets.get(bucketIdOfMinimum)[indexOfMinimum];

        int i = 0;
        for (int current[] : dataBuckets)
        {
            // System.out.println("\n\n NonThreaded Searching Bucket # " + i);
            for (int k = 0; k < current.length; k++)
            {
                if (current[k] < currentMinimum)
                {
                    bucketIdOfMinimum = i;
                    indexOfMinimum = k;
                    currentMinimum = current[k];
                }
            }
            i++;
        }
    }

    // Assign each thread a bucket to search
    void threadedSearch(List<int[]> dataBuckets, int results[]) throws InterruptedException
    {
        threads = new ArrayList<Thread>();
        for (int i = 0; i < dataBuckets.size(); i++)
        {
            MyRunnable task = new MyRunnable(dataBuckets.get(i), i , results);
            Thread worker = new Thread(task);
            worker.setName("" + i);
            // System.out.println("\n\n Thread # " + worker.getName() + " will search Bucket # " + i);
            threads.add(worker);
            worker.start();
        }

        for (Thread thread : threads)
        {
            thread.join();
        }
    }

    // Object (the "task") passed to a thread constructor
    static public class MyRunnable implements Runnable
    {
        private int data[];
        private int results[];
        private int threadId;

        MyRunnable(int data[], int threadId , int results[])
        {
            this.data = data;
            this.threadId = threadId;
            this.results = results;
        }

        // Code is that executed by the thread
        @Override
        public void run()
        {
            int indexOfMinimum = 0;
            for (int i = 0; i < data.length; i++)
            {
                if (data[i] < data[indexOfMinimum] )
                {
                    indexOfMinimum = i;
                }
            }
            results[threadId] = indexOfMinimum;
        }

    } // end inner class MyRunnable

} // end class

  • « cherokee web server django
  • Concurrency Multithreaded Advanced ExecutorService Callbacks »

Published

Nov 27, 2012

Category

java

~387 words

Tags

  • concurrency 10
  • java 252
  • multithreaded 5
  • simple 11