Galcon Games
Phil Hassey - game dev blog
Phil Hassey as Wolverine
"What kind of
arrogant jerk
has a website like this?"

Android threading tips

Hey,

I still don’t completely understand how the Android threading works. So that explains some of why my code sometimes has trouble. Here’s a good article on deadlocking in Java.

In a Activity + GLSurfaceView there are at least two threads in operation. In my code I synchronize all my native methods. Some of my native methods have to call back to Java to load textures, play music, etc. I’ve found in certain cases a deadlock occurs. My guess is that the deadlock happens when:

– I’m receiving a touch event and I’m trying to call my native code with the event data
– I’m rendering a scene and it is trying to load a texture.

My guess is that loading a texture is trying to acquire a lock that the Activity thread has locked, while the Activity is trying to send an event to the native code which is locked.

Here are ways to push code onto the other thread so that you can reduce the amount of synchronized type deadlocks.

Putting a item on the Activity / UI thread

            myActivity.runOnUiThread(new Runnable(){
                public void run() {
                    /* do something in the Activity UI / thread */
                }});

Putting an item onto the GLSurfaceView thread

        myGLSurfaceView.queueEvent(new Runnable(){
            public void run() {
                /* do something on the GLSurfaceView thread */
        }});

Anyway, hope that helps!
-Phil

3 Responses to “Android threading tips”

  1. Phil Hassey » Blog Archive » Android threading tips | Android-share Says:

    […] the RSS 2.0 feed. you can leave a response, or trackback from your own […]

  2. Phil Hassey » Blog Archive » Android threading tips | Android-share Says:

    […] to this entry through the RSS 2.0 feed. you can leave a response, or trackback from your own […]

  3. Phil Hassey » Blog Archive » Android threading tips | Android-share Says:

    […] to this entry through the RSS 2.0 feed. you can leave a response, or trackback from your own […]