jeudi 26 mars 2015

Where to use Synchronized when running Threads in seperate methods



I have done some research and could not find a solution to this problem.


From this topic Synchronization, When to or not to use? i understand i could use synchronized, but doing so doesn't solve the problem.


The case is that i have a method in which a Thread is used to create an ArrayList. In that same Thread another method is called after a BufferedReader has finished reading a file and the lines are being added to the first List.


In the second method the first list is being used to create the second List. When all that is done, the first method uses the second list.


This is somewhat the code i use, if there is something not clear please ask and i will try to provide the info needed.



public synchronized void theBaseList() {
Thread t = new Thread() {
@override
public void run() {
try(
while((line = br.readLine()) != null) {
firstList.add(line):
}
}
nextMethod();
currentObject = (Object[]) secondList.get(0); // throws an exception
}
}
};
t.start();





public synchronized void nextMethod() {
Thread t1 = new Thread(){
double objectListSize = calculateObjectListLength(firstList.size());
@override
public void run() {
try {
// create Objects
secondList.add(objects);
}
}
};
t1.start();
}


When i use a Thread in nextMethod() to create a new list of Objects from the items in the first list, i get an ArrayIndexOutOfBoundsException saying



Exception in thread "Thread-4" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0



I avoided this by not using a Thread in the second method and all works fine.


If i do use 2 Threads and make both methods synchronized, it still throws the exception.


Is it possible or should i just settle by not using a Thread in the second method? I thought synchronized was for dealing with this sort of problems. I don't understand why it doesn't work.




Aucun commentaire:

Enregistrer un commentaire