I developed some applets that at a fixed interval, say 1 second, update a value on the page.
The pages with the applets are displayed in a frame, and the frame page structure has one frame
on the left, with the menu links, one frame on the bottom, with another applet running continuosly,
and a main frame taking the major part of the screen. This last frame shows a page with the updating
applets described above. There are a number of pages of this kind, and you switch between them using
the menu links on the left frame. The chosen page is always shown in the main frame.
All is running well, but when I switch from an applet page to another, using the links on the left frame,
most of times the applets of the page left behind don't stop, thus adding their execution to the
applets on the new page and leading to a performance degrade of the browser, often locking it.
Instead if I switch to a page without frames, all the applets always stop.
Some of the applets of concern create threads, some not.
To stop the applets that create threads, I used this technique:

public class A2 extends Applet implements Runnable {
private volatile Thread runner;

...

    public void destroy() {
    }

    public void start() {
        if (runner == null) {
         runner = new Thread(this);
         runner.start();
        }
    }

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

...

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (runner == thisThread) {
           ... // value update jobs
           ...
           try { Thread.sleep(TickAnim); }
           catch (InterruptedException e) { }
          }
        }
    }
...
}


About the applets that don't create threads, I tried this way:


public class A1 extends Applet {
private Timer timer;
boolean TimerON = false;
private int delay = 1000;

...

    public void destroy() {
    }

    public void start() {
        TimerON = true;
        timer = new Timer();
        timer.schedule(new TimerTask() {
            // overrides the run method to provide functionality 
            public void run() {
                if (!TimerON) return;
                ... // value update jobs
                repaint();
            }
        }
            , delay, delay);
    }

    public void stop() {
        TimerON = false;

...
}

I'm using Internet Explorer, but I tried other browsers, like Netscape, Mozilla, Firefox, Opera, and all
act the same.

The Java version is 1.5.0 (build 1.5.0_04-b05).

The problem arises even on different PCs, exploring the same pages.
Can anyone help me to solve this problem?

Thanks
Maurizio

I think stop was deprecated. Anyways, try setting some kind of flag to stop the execution of the thread in the run method.

If (this is true)
{
    Thread = null;
}
else
{
   Thread = Thread.currentThread();
}

That's my guess at it.

Stop is deprecated ages ago because it's extremely dangerous.
It potentially leaves a lot of objects in undeterminate states.

I'd not be surprised if Sun has resolved the native part of the code to silently fail and not actually do anything for that reason (they've done so with several other inherently unsafe operations that were deprecated).

I fixed the problem.
Bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.