views:

64

answers:

1

Hi all.

Here is my situation: I am building a game for android and my game's activity is composed of a custom surfaceView which has a thread for the game logic and rendering. The architecture is similar to the LunarLander demo from Google's website.

When the activity starts it creates the surfaceView and calls this method:

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {   
        renderThread.start();
    }

When I press the home button to exit the game, the onPause() method is called, which calls surfaceDestroyed(). In surfaceDestroyed I stop the game Thread by calling:

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        synchronized(holder)
        {
            renderThread.stop();
        }
    }       

The app goes to background fine. Then, when I relaunch the app by pressing the icon, I get a "Thread already started" message in the log along with a "force close" popup on the screen. This message happens when the activity enters the "surfaceCreated" method when it calls start() on the render thread.

Now I've looked into it for hours and can't figure out why this is. I believe my thread is stopped when I close the app so I don't understand why it says it has already started.

Any help would be greatly appreciated. Thanks.

bye.

+1  A: 

Those methods don't do what you think they do. From the API doc:

It is never legal to start a thread more than once.

And:

public final void stop() - Deprecated. This method is inherently unsafe.

If you want to pause a thread, you have to use Object.wait() and Objecft.notifyAll() from within the thread.

Michael Borgwardt
Hi, thank you for the answer.I replaced stop() by a join() in a while loop.I believe it waits before stopping the thread. But I still have the same problem, am I missing something ?As for the start of thread. I understand that the thread cannot be started more than once hence the error message. So do you think that I get this error just because the thread is not properly stopped ?Thanks
NioX5199
@NioX5199: no, join() does not stop the thread either, it waits for it to finish on its own. You get the error because the Thread is already started. Threads *cannot* be "stopped" and restarted. As I wrote: for making a thread pause until a condition is fulfilled, you use Object.wait().
Michael Borgwardt
He should probably not do it that way either. Recreating the thread is probably a better approach.
alexanderblom
Ok thanks. As a workaround I check the status of the thread before restarting it and if its status is TERMINATED I just recreate it. I'm not sure if it's really what I want to do here though.
NioX5199