Thread Problem?

With the following code i sometimes an excption:
java.lang.IndexOutOfBoundsException: Index: 20, Size: 20 at the get(i) line


public void processCollisons(Octree aOctree,List<LeafCollisionSupportIF> leafList){
...
for (int i=leafList.size()-1; i>=0;i--){
    LeafCollisionSupportIF leaf=aLeafList.get(i);
    ...
}
...

how is that possible? I think its a Thread problem but how to debug this. I already tryed a synronized Collection (

Collections.synchronizedList(new ArrayList<LeafCollisionSupportIF>())

) and make the method synchronized but without success.
I also cant reproduce this in a simple unittest.
Any tips how to debug this?

Synchronizing the list protects the internal state of the list. If you make two calls to the list, eg one size and one get, there is time between the calls that other threads can call methods on the collection. That is happening to you:


for (int i=leafList.size()-1; i>=0;i--){
    // Before this next line, some other thread removes from leafList.
    LeafCollisionSupportIF leaf=leafList.get(i);

Synchronizing the method protects two threads from entering the method at the same time. If the code removing from the list is in a different method that is not synchronized, this doesn’t protect access to the list. Remember synchronizing the method is the same as wrapping the method contents with “synchronized (this) {…}” (or “synchronized (Whatever.class) {…}” for static methods).

Synchronize on the list to protect the entire block of code:


synchronized (leafList) {
    for (int i=leafList.size()-1; i>=0;i--){
        LeafCollisionSupportIF leaf=leafList.get(i);
}

Does your app really need multiple threads? Sometimes you can simplify things, eg, say you have a game thread and a network thread. Read in objects on the network thread and store them for processing on the game thread to avoid any synchronization issues.

i use the default jogl setup (Animator GLPanel) so i think i have atleast 3 threads: mainthread, EDT and Animator(render loop) right?

i tryed to illustrate the problem in the following picture
engine thread problem picture

Eh, I use LWJGL. I have one thread for rendering and one for network.

My suggestions still stand: either synchronize properly, eliminate using multiple threads, or move the concurrent modifications to occur in the same thread.

Note you must synchronize on the list in both threads, not just where shown above.

To echo this -

I will usually make a “pending” version of each list that gets processed in the main game loop. That means that any other threads never actually change game data directly, instead they put their change into the pending list. This not only avoids most possible issues but it also helps maintain sanity - your stack traces will make more sense (all being on the same thread) and you can keep everything as “deterministic” as possible.

Problems like this are virtually impossible to eliminate as long as threads are in use. The “standard”
architecture of a java game will have at least 4 threads running freely through all your data structures.

(1) the game main thread
(2) the mouse / keyboard event handler
(3) the network communications handler
(4) the screen refresh

That is complete madness. You must have one thread that does all the work, and has events from
all other sources presented in a disciplined way through queues.

An easier way to deal with threads and communications between them is have a look at java.util.concurrent*. I often have a ConcurrentLinkedQueue that one thread adds things too and another thread that removes them. The nice thing about this is, that having two threads that add objects to the queue requires not extra synchronization. There are also things like fairness that can be set with the concurrent package which is tricky to implement with monitors.

Personally I use threads quite a bit. I find java the easiest language to deal with threads, and of late the performance is very good.

It should be noted there are a number of extra tricks that can be done. But IMO java.util.concurrent* has made most of them obsolete.