f



PThread Problem (Urgent Help Needed)

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
the concurrency package. I am in urgent need to get this thing working
in the next few days. I am new to this and any help will be brilliant.

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, TargetCounter counter, int target,
int arraysize)
      {
    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];
   }
  } 
}
0
rich.wak (7)
1/3/2004 12:33:04 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

0 Replies
605 Views

Similar Articles

[PageSpeed] 12

Reply: