Java Thread Problem (PThread)

From: R Mar (rich.wak_at_ic24.net)
Date: 12/30/03


Date: 30 Dec 2003 11:49:07 -0800

Hi, im trying to write a program that uses threads to search through
an array of random numbers and then returns the total times that a
specified target number occurs. The set up is that the user inputs the
size of the array, the number of processors that they wish to simulate
(number of threads created), and the target number that they want to
search. The program then populates the array with random numbers,
creates and runs the threads to search through array and increments
the total each time that the target number is found.

im having trouble writing the code to create the threads and run them,
and also counting the number of times the target number occurs
(including the mutal exclusion so that count is correct).
Any suggestions, this is what i have so far. - it needs to use
concurrency.

import java.io.*;
import java.util.Date;
import Concurrency.*;

public class ParallelArray{
        
        public static Semaphore s = new Semaphore();
        private Date timeObject;
        private long beginTimer, endTimer, totalTimeTaken;
    private int target,total,arraySize = 0;
        private ProcessorThread[] threadsGroup;
        private int processorNumber;
        private int randomInt;
                
        public static void main(String[]args) throws Exception
                {
                        ParallelArray ParaArray = new ParallelArray();
                        ParaArray.run();
                }
        
        public void run()
                {
                        System.out.print("Enter Array Size Value: ");
                    arraySize = getInt();
                        int randomArray []= new int [arraySize];
                    for (int i = 0; i< arraySize; i++)
                        {
                                int arrayContent = getRandomInt();
                                System.out.println("Random Number: "+arrayContent);
                        }
            
                    System.out.print("Number of Processors To Simulate (1-8): ");
                    processorNumber = getInt();
                    
                    System.out.print("Enter Target Search Number: ");
                    target = getInt();

                           TargetCounter counter = new TargetCounter(randomArray);
                    threadsGroup = new ProcessorThread[processorNumber];
                    timeObject = new Date();
                        beginTimer= timeObject.getTime();

                    for(int i = 0; i < processorNumber; i++)
                        {
                    
                            ProcessorThread threadsGroup = new ProcessorThread(i);
                                //threadsGroup.start();
                        try
                        {
                            threadsGroup.join();
                            System.out.println("Simulating Processor No: ["+(i+1)+"]");
                        }
                        catch (Exception e) {}
                        }
                
                timeObject = new Date();
                endTimer = timeObject.getTime();
                
                totalTimeTaken = (endTimer-beginTimer);
                        
                counter.DisplayTotalTarget();
                System.out.print("\n"+"Target Number of: "+target+" occurs "+total+"
time(s)");
                                                                                
                        System.out.println("\n"+"Total Time Taken: " +totalTimeTaken+ "
millisec/s.");
                        System.out.println("Compuation Time per Processor: "
+((totalTimeTaken)/processorNumber) + " millisec/s.");
                }

                public static int getRandomInt()
            {
                      int randomInt = 0;
                      randomInt = (int)(Math.random()*11);
                     return randomInt;
            }

                private int getInt()
                {
                        int Num;
                        String inStr = null;
                        try
                        {
                                BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
                                inStr = in.readLine();
                        }
                        catch(IOException ioe)
                        {
                                System.out.println(ioe);
                        }
                        Num = Integer.parseInt(inStr);
                        return Num;
                }
}

class ProcessorThread extends PThread
{
        private TargetCounter counter;
        private int threadID=0;
        private int target,arraySize=0;
        private ParallelArray arrayClass;
                   
         public ProcessorThread(int ID)
      {
                  threadID = ID;
                  counter = counter;
                  target = target;
                  arraySize = arraySize;
            
           arrayClass = new ParallelArray();
      }

        public void run()
        {
                int countArray[];
                int totaltarget = 0;
            do
                {
                countArray = counter.getCount();
                        if (countArray[0] < arraySize)
                        {
                                if (countArray[1]==target)
                                {
                                        totaltarget++;
                                }
                                counter.setCount(countArray[0], totaltarget);
                                totaltarget = 0;
                        }
            }while( countArray[0] < arraySize);
    }
}
class TargetCounter
{
        private int total, grouptotaltarget=0;
        private boolean locked = false;
        private int[] totaltarget; //cess
        private int[] countArray;
        private int[] randomArray;
        
        public TargetCounter(int[] random)
        {
                totaltarget = new int[random.length];
                randomArray = random;
                countArray = new int[3];
        }
        public synchronized void setCount(int countArray, int newTotal)
        {
                while( !locked )
                {
                        try
                        {
                                wait();
                        }
                        catch( InterruptedException e )
                        {}
                }
                grouptotaltarget++;
        
        totaltarget[countArray] = newTotal;
                
                locked = false;
                notify();
        }
        public synchronized int[] getCount()
        {
                while( locked & ((grouptotaltarget < totaltarget.length)) )
                {
                try {
                                wait();
                        }
                        catch( InterruptedException e )
                        {}
                }
                
                countArray[0] = grouptotaltarget;
                if(grouptotaltarget < randomArray.length)
                        countArray[1] = randomArray[grouptotaltarget];
                        
                if(grouptotaltarget == 0)
                        countArray[2] = 0;
                else
                        countArray[2] = totaltarget[grouptotaltarget - 1];
                                
                locked = true;
                notify();
                
                return countArray;
        }
                public void DisplayTotalTarget()
                {
                        for(int i = 0; i < totaltarget.length; i++)
                        {
                                total = total+totaltarget[i];
                        }
                }
}



Relevant Pages

  • Fwd: Please Forward: Ruby Quiz Submission
    ... string then back into a count array. ... beyond the range of the target and source, I would munge one or more ... the target contains a direct representation of the pangram search ... def to_counts ...
    (comp.lang.ruby)
  • Re: readdir formulated badly? gives wrong count
    ... Sometimes there might be some numbered files already in the target ... I'll probably try to turn the perl script into something more general ... You are doing boolean tests on the contents of array elements when you ... sub usage { ...
    (perl.beginners)
  • Re: Efficient Searching
    ... by creating an array from the ... Alternatively, I create a hash with integer_1 as key and as the value, ... Thus for 200 target values, ... cannot program a binary search correctly, ...
    (comp.lang.perl.misc)
  • Fwd: Please Forward: Ruby Quiz Submission (Part 1)
    ... array by initializing it with complete information on what was knowable ... string then back into a count array. ... beyond the range of the target and source, I would munge one or more ... the target contains a direct representation of the pangram search ...
    (comp.lang.ruby)
  • Re: memory mapped file question
    ... source copies its data to a special address and a target can read out ... data from this special memory address and has to copy it into its ... It allows any number of processes to share a block of data, overlaying a VB array of any type. ... dim MyArrayas Type MyArrayType ...
    (microsoft.public.vb.winapi)