CIS 121 March 17, 2000


Let us examine more closely some of the aspects of applets. Recall that there are several basic methods we can override when we create an applet.
  1. public void init()

  2. public void start()

  3. public void stop()

  4. public void destroy()

The init() method is called one time when the applet is started. So we should not put any code in the init() method that needs to be run again.

The start() method is called each time the applet becomes visible. For example, if we change to another page with our browser, and then come back to our applet, then the start() method is run again. Also if we minimize the window in which the browser or applet viewer is running, then maximize again, the start() method will be run again.

The stop() method is called each time the applet becomes invisible. Again if we change pages with our browser or minimize the window in which the browser or appletviewer is running, the stop() method is run again.

The destroy() method is called one time when we exit the browser or appletviewer. This method is similar to the finalize() method. It is important if we are running threads, because when an applet exits, we also want to stop any threads the applet started.

Let us look at an example to illustrate when these methods are run.

Another important feature of using applets is that values can be passed to an applet from the HTML code we use to call it. This allows us to make changes to values in the applet without having to recompile the source code.

In order to do this we use code similar to the following in our HTML document.

<applet code=Example2 width=500 height=300>
   <param name="width" value="100">
   <param name="height" value="200">
</applet>
Let us look at an example.

Let us again look at the concept of Threads. In order to accomplish this we need to first look at the concepts of Threads.

Threads

Recall that a thread is a single sequential flow of control. In all of the programs that you wrote in CIS 120 and this the first part of this semester ran in their own thread. Java allows us to have more than one thread executing simulataneously. Since the CPU can only process one thing at a time, multithreading is accomplished by very quickly switching threads in and out of the processor.

Multithreading is sometimes necessary when developing applets. Recall that an applet cannot have a main method, and, therefore, must be run in the context of a browser or appletviewer. The browser/appletviewer runs in its own thread, and so we must make sure that the methods in the applet do not take a long time to execute. In particular, we shouldn't have infinite loops in the methods of an applet if we intend to have that applet run in the thread of the browser/appletviewer.

The following is a partial sequence of execution when an applet runs in the thread of a browser/appletviewer.

The browser/appletviewer loads the applet
The init() method in the applet is executed
The start() method in the applet is executed

The class Thread is found in the java.lang. Since each applet we create extends the class Applet, we can't make the applet a subclass of Thread. In order to run an applet in its own thread we implement the interface Runnable. The Runnable interface has one method which must be overridden, public void run(). This method is run when the thread is started.

There a few methods available in the Thread class that are important. Suppose we have a Thread object called thread.

thread.start() causes the thread to begin executing. The run() method is then called.
thread.suspend() temporarily halts the execution of the thread.
thread.resume() resume execution of the thread
thread.stop() permanently stops the execution of the thread

There is a static method called sleep in the class Thread which takes as an argument a number of milliseconds. We can use this method to cause the Thread to sleep for a number of milliseconds. This method must be called in a try block since it might cause an InterruptedException to be thrown. This occurs if the user halts the applet while the thread is sleeping.

Let us now examine some of the exercises you looked at in lab yesterday.