I am returning an array list form an object and using it in another. The application is multi-threaded and each thread fills the array list one int at a time from a file, so each add is a get to the array list. There are 200 threads with a file of 1million ints each. The application takes hours to run and I assume this is my bottle neck, since when I test with a local array list it takes 4 minutes. My problem is, this is used everywhere and I need to synchronize on the array list. Is there a fast solution to this problem or do I have to make it so each thread has its own array list and don't return it?
Actually I was wrong, its only when the array is local to the method that is faster anywhere like declared at top of class it takes hours to run, I'm stumped on this.
My return code looks like:
synchronized public ArrayList<Integer> getData()
{
return this.myData;
}
Here is what runs slow, I removed other things and am trying to bench mark on this and this takes hours:
Scanner scanner = new Scanner(filePath);
/*
* While we have data keep reading
* when out of data the simulation is complete.
*/
while (scanner.hasNext())
{
/*
* Get the data to simulate requests
* and feed it to the algorithm being evaluated.
*/
if (scanner.hasNextInt())
{
int temp = scanner.nextInt();
//System.out.println( this.tClientName+" "+temp);
/*
* Add the temp value from incoming stream.
*
* todo:: UNLESS its NOT found on the client as a miss
*/
tClientCache.getCache().add(temp);
}
else
{
scanner.next();
}
}//END Of while (scanner.hasNext())
/*
* Close the scanner
*/
scanner.close();