public void surfaceCreated(SurfaceHolder holder) {
   if (!running) {
     running = true;
     thread.setMode(Mode.RUNNING);
     thread.start();
   } else {
     thread.resume();
   }
 }
  public void surfaceDestroyed(SurfaceHolder holder) {
    thread.setMode(Mode.STOPPED);

    boolean retry = true;
    while (retry) {
      try {
        thread.join();
        retry = false;
      } catch (InterruptedException e) {
      }
    }
  }