Reputation: 1657
I have a method in my Util class that used to work but however for some reason it is not working. I am All variables accessed by the class are static and are in the same class, however it prints out wrong. My question could be answered simply if the problem is simple
public static List<Integer> superMaleList = new ArrayList<>();
int REDO_LISTS = 2;
public static void runRefresh(int runcode)
{
if (runcode == REDO_LISTS)
{
Log.i("RUN REFRESH", " " + superMaleList.size()); //Prints out correct size
refresh(runcode, superMaleList);
}
}
public static void refresh(int runcode, List maleList)
{
Log.i("RUN REFRESH", ""+ maleList.size()); //prints incorrectly
}
These methods are accessed staticly in another class. I can add to the list and read from the list in other classes, however when I pass the list variable to these methods, they do not print correctly. Why?
UPDATE
The way I use these methods are as follows (this is performed staticly in a thread)
EventUtils.superMaleList.add(2);
EventUtils.runRefresh(2);
Upvotes: 1
Views: 98
Reputation: 139
Calling superMaleList directly in my opinion is bad, i am not sure why you are trying to do this. GKNICKER has stated it perfectly why so you are getting the error.
EventUtils.superMaleList.add(2);
EventUtils.runRefresh(2);
Suppose there is some thread with
EventUtils.superMaleList.add(2);
EventUtils.runRefresh(1);
Another element is added but runRefresh will do nothing
Instead of the above you should make superMaleList as private and add elements via some method which is static and synchronized.
Upvotes: 1
Reputation: 5568
Possibilities:
In a multi-threaded environment, you may be removing elements from superMaleList
on another thread, calling clear()
on the list, or re-initializing superMaleList to a new list object. You could prevent the last condition (re-initializing) by making superMaleList a final
variable.
Multiple class loaders can result in more than one superMaleList. Try configuring your application to use a single ClassLoader, to see if that makes a difference.
Upvotes: 1