?
Solved

How to destroy applet when browser leaves document

Posted on 1997-08-17
5
Medium Priority
?
885 Views
Last Modified: 2008-03-10
I have made an applet which works fine. Except that when the same page is called in another browser window, the applet interferes with the applet in the original window. This happens even when the original window is closed, or now contains a different document without the applet.

I have checked some things, and the main thing I found out was that the applet is not destroyed when the browser leaves the document. It remains in memory !!

My question is how do i make the browser destroy the applet when the browser leaves the document with the applet.

I suspect that the browser keeps all the applet in memory  as long as the applets are not explicitely destroyed. This might explain memory problems after visiting a couple of pages with heavy java-applets.


0
Comment
Question by:sybe
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
russgold earned 300 total points
ID: 1225190
First of all, your suspicion is correct. Applets are not destroyed when the browser leaves the document with the applet. This allows you to return to that applet later by simply hitting the 'back' button.

It also means that hidden applets can continue to run, sucking up CPU cycles.

There is a standard way to address this problem:  When the browser leaves your applet's page, it calls the 'stop()' method of your applet. You should override this method to kill any active threads, to prevent them from running while your applet is hidden.

If you want, you can also override the 'start()' method to resume any killed threads. It will be called when your applet starts, and again if the browser ever returns to your applet's page.
0
 
LVL 28

Author Comment

by:sybe
ID: 1225191
I do understand your suggestion, but it does not solve the problem in my case, because in my applet I use a thread which is stopped and started depending on the "state" of the applet. That means I already have the start() and a stop() overriden.

The best way would be to check how the stop() method is called: if it is from the applet itself, then don't destroy the applet, if it is from the browser then do destroy the applet.

Is it possible to pass arguments to the stop() method ?
and does the browser pass any arguments ?

For the time being I leave the question open, though your answer gave me some ideas to try on.
0
 
LVL 4

Expert Comment

by:russgold
ID: 1225192
Then perhaps some clarification is in order. You described two problems: 1. New applets interfere with old, and 2. Applet memory is not reclaimed.

With regard to the second: Browsers have total control over when to reclaim memory from applets. Currently, most do not - ever.  You cannot really control that, since only the browser knows whether your applet will ever be restarted.

As to the first: if you are really stopping your thread in your Applet's 'stop()' method, there should be no interference  (Please distinguish between the Applet method and the similarly named method on a Thread).  Your comment that you need to tell when the *browser* calls the method and the fact that you are seeing interference makes me think otherwise.

What exactly do you mean by the Applet's "state" - and how do you detect changes in it to kill threads?

0
 
LVL 28

Author Comment

by:sybe
ID: 1225193
Below is the part of the code that i talk about. A mouse events triggers a new thread, that keeps on going as long as no other event interferes. When that happens, the stop() is called and the thread is killed.

When the browser leaves the page, the stop() method is called too, which is ok, but i would like to destroy the applet in that situation.

If you state that it is impossible to write code that does that, i will believe you, and consider my question as answered.



public boolean mouseUp(java.awt.Event evt, int x, int y) {

      -- do something --

      Situation = "on";

      start();
      return true;      
}


public void start() {
      workThread = new Thread(this);
      workThread.start();
}

public void run() {
      while (Situation == "on") {
            -- get a variable from somewhere --
                  if ( -- variable == something) {
                              do something
                              Situation = "off";
                        }
                  }
            try {Thread.sleep(SleepStaticRead);}
            catch (InterruptedException e) { }
      }
      stop();
}

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

0
 
LVL 4

Expert Comment

by:russgold
ID: 1225194
1. Java garbage collection ensures that objects will not be destroyed as long as *any* references to them exist. Since the browser holds a reference to your applet, it will prevent it from being destroyed until *it* wants it to be. As long as you kill your threads appropriately, it should not matter.

2. It does appear that you are killing your threads when the browser leaves the applet, so I don't really understand your comment about applets interfering with each other.

3. If you do want to take different actions on applet exit from your program determining that a thread should stop, you can make a pair of new methods which control the thread and call them from both the stop/start methods and your mouseUp/run methods.  start/stop can then take additional steps.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question