snairsumith / beginning-android-games

Automatically exported from code.google.com/p/beginning-android-games
0 stars 0 forks source link

Chapter 4, pg: 180, Kindle edition: "while (true) {thread.join()}" ? #63

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi Mario,

Thanks for the excellent book. It is very interesting and enjoyable to read.

I have this code piece I didn't understand:

public void pause() {
  running = false;
  while(true) {
    try {
      renderThread.join();
    } catch (InterruptedException e) {
      // retry
    }
  }
}

For me it looks like an infinite loop, just tried a simple Java example and it 
seems that thread.join() would work fine only without "while(true)", like this:

    public void destroy() {
            System.out.println("Thread will end!");
            try {
                thr.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        System.out.println("Thread has ended!");
    }

Adding the "while(true)" will either go in infinite loop or trigger exception 
"unreachable code".

Is this some specific trick to Android?

Many thanks!
Mihai

Original issue reported on code.google.com by rumbur...@gmail.com on 16 May 2012 at 9:25

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I've the same problem.

He has fixed it in the source code: 
http://code.google.com/p/beginning-android-games/source/browse/trunk/ch04-androi
d-basics/src/com/badlogic/androidgames/SurfaceViewTest.java - where he has 
added a break statement after the renderThread.join() - Then the pause code 
will wait until the render thread dies (renderThread.join()). If it gets 
interrupted, it catch the exception and try again! Else the render thread is 
died and break out of the while loop.

Original comment by fuddiG on 18 May 2012 at 1:43

GoogleCodeExporter commented 8 years ago
my bad, this was addressed in ticket 24. solution is:
"It's necessary to add a break after renderThread.join() call."
thanks!

Original comment by rumbur...@gmail.com on 20 May 2012 at 4:26