Problems cleaning up an applet

i have an applet using one thread for drawing graphics.
the problem is when i use appletviewer with console window in background and i close the applet window, the onsole frame seems to lock for a while.

the code:



        public void start()
      {
            if (runner == null)
            {
                  runner = new Thread(this);
                  runner.start(); 
            }
            System.out.println("Applet : Start");
      }

      public void stop()
      {
            System.out.println("applet stop");
            runner = null;
      }
      
      
      public void destroy()
        {
             offscreen.dispose();
             System.out.println("destroyed");
         } 

im in sorrow that the thread doesnt get destroyed properly.

Do you exit your run() function when runner is set to null? You must do something like this:


public void run() {
   // exit game loop when stop() is called.
   while (runner != null) {
   }
}

When you exit the run() function the thread will stop.

i added the code line, but finishing the applet still seems to take to long. its only a matter of ~10 sec. but if i run a applet with no extra thread it clears up immediately

The only other thing I can think of is seting runner to be a daemon thread:


runner = new Thread(this); 
runner.setDaemon(true);
runner.start(); 

From setDaemon javadoc: “The Java Virtual Machine exits when the only threads running are all daemon threads.”

I always call stop() on the animation thread and I’ve never seen the problem you describe.


public void stop ()  {
            if (animation != null)  {
                  animation.stop();
                  animation=null;
            }
            
}

[quote]I always call stop() on the animation thread
[/quote]
tsk… tsk…

Using deprecated methods… sigh…

You should interrupt the animation thread and let it exit gracefully.
I usually have something like this for my animation loop:


try {
  while( !Thread.interrupted() ) {
    doStuff()
    Thread.sleep( x );
  }
}
catch(InterruptedException e) {
  // just exit
}

In the applet stop() method:


animThread.interrupt();
animThread.join();

[quote] Using deprecated methods… sigh…
[/quote]
Oops, I didn’t know stop() was deprecated; I wondered why I kept getting those deprecated warnings, but didn’t care enough to compile with the correct flag to see what it was :-/ . Yes, I’m lazy like that…