Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 8: Threads, Netscape, and the resize problem

How to deal with applet resizing in Netscape Navigator

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

If start() is called again, then it means that this was only a resize. Stop and nullify the killThread before it wrecks everything. Resume all the suspended threads, and continue running.

Here are the final versions of the applet's start() and stop() methods:

    public void start() {
    // If we were in the middle of kill thread  
(caused by resizing)
    // stop the killthread and start (i.e. resume)  
extraThread.
    // Otherwise just start as normal
    if (killThread != null) {   
        killThread.stop();
        System.out.println ("KILLTHREAD STOPPED");
        killThread = null;
    }
    // Now start running the extraThread thread
    if ( extraThread == null ) {
        extraThread = new Thread(this);
        extraThread.start();
    }
    else
        extraThread.resume();
    }
    public void stop () {
    // First suspend the extraThread thread
    if ( (extraThread != null) &&  
(extraThread.isAlive()) ) {
        extraThread.suspend();
    }                   
    // Fire up a killthread, which will kill the  
extraThread thread
    // in 5 seconds unless start is called
    if (killThread != null)
        System.out.println ("ERROR - killThread  
shouldn't exist");
    else {
        killThread = new KillThread(this);
        killThread.start();
    }   
    }


(NOTE: This assumes that we keep track of killThread and extraThread as object variables somewhere, which we would most likely be doing anyway.)

There you have it! With this stuff installed, you will never mess up an applet by resizing a Netscape browser again.

You might want to experiment with other browsers, although if they don't do things the way Netscape does, chances are this code will not affect things adversely. Also, the 5-second time limit may need to be shortened or lengthened, depending on the speed of different systems. So far, 5 seconds seem to do it okay on all the systems we've worked with.

I've seen a lot of applets out there that will "break" if you resize your browser window; this fix should put an end to that problem. If anyone knows of a more elegant solution besides launching a killThread, I'd love to hear about it! Happy coding.

About the author

John D. Mitchell is yet another UC-Berkeley Computer Science graduate weaned on caffeine, sugar, and way too little sleep. He spent three years toiling on PDA software at Geoworks for stock that is actually worth more than wallpaper. After the first public release of Java, John bailed out of his day job to develop applets and a Java compiler. He funds his Java addiction by writing compilers, TCL/TK, Perl, C++, and Java systems. His copious spare time is consumed by moderating the comp.lang.tcl.announce newsgroup and writing an awesome Java book.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources