Reputation: 401
So I have a List
public static List<Position> chests = new ArrayList<Position>();
in my main class, but inside this main class, I have an Anonymous Class (a thread) which uses this list. For whatever reason, this thread cannot correctly read info from this list. Here's my other code for that Anonymous Class:
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < ChestGrabber.getChests().size(); i++) {
Position chest = (Position) ChestGrabber.getChests().get(i);
if (player.getDistance(chest.x, chest.y,
chest.z) <= 4.7F) {
openChest((int) chest.x, (int) chest.y, (int) chest.z);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// chests.remove(chest);
}
}
}
}).start();
I have tried to access the list a few different ways:
ChestGrabber.getChests().size()
or
getChests().size()
or
chests.size()
.. and none of these work: they always return 0
, so the for
-loop is skipped completely.
Am I overlooking something? I can't seem to get this to work.
Upvotes: 0
Views: 88
Reputation: 32535
You are invoking chests.clear();
right after starting new thread (but that doesn't mean that thread is running after invoking start()
) so the list is probably wiped out before new thread tries to read from it.
Upvotes: 1
Reputation: 6260
Everything seems right. Try using an Iterator instead. And Since you are getting Something during the first call of ChestGRabber.getChest, save that in a list and the iterate over the same list in the for loop. It should have worked. Try what I told you, otherwise everything is right. Try Deleting your meta data of workspace and import project again(if ur using eclpise that is), try cleaning project. your code is correct
Upvotes: 1