Java Applets and Threads

This first assignment of the Internet programming 1 - course was ment to give a basic understanding of some important Java network programming techniques.

Its purpose was to:

In Internet programming - and network programming in general - it's very important to understand what threads are, and how to use them. Therefore I focused more on what I've learned about threads than the other techniques listed above.

The description and my solution of the Java Applets and Threads -assignment can be viewed and/or downloaded at the bottom of this page.

What is an Applet?

An applet is an application that is intended to be embedded in another application, usually a Web browser.

An applet that is to be embedded in a Web browser and displayed on a Web page must subclass class Applet.

The Applet class provides an interface between applets and their environment.

What is a Jar File?

The JavaTM Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file will contain the class files and auxiliary resources associated with applets and applications.

What is a Thread?

Sequential applications are the most common applications, and most programmers are familiar with them. If you're a programmer you've probably written an application that displays "Hello World!"; this is sequential application.

In other words, each application has a beginning, an execution sequence, and an end. At any given time during the runtime of an application, there is a single point of execution.

A thread is similar to the sequential application. A single thread also has a beginning, a sequence, and an end. At any given time during the runtime of the thread, there is a single point of execution.

However, a thread itself is not an application since it cannot run on its own. A thread must have an application to run within.

The excitement surrounding threads isn't about a single sequential thread. It's about the use of multiple threads running at the same time and performing different tasks in a single application.

The Web browser you are using to read this text is propaply an example of a multi-threaded application. Within the typical Web browser, you can scroll a page while it's downloading an image or an applet, execute a javascript, play animations and sound concurrently, print a page in the background while you request and download a new Web page...etc.

This is Sun's definition of a thread:

"A thread is a single sequential flow of control within a program."

What are Java's Class Thread and Runnable Interface?

There are two ways to create a thread with Java: by using class Thread or by using the Runnable interface.

Subclass class Thread:

Declare a class to be a subclass of Java's class Thread. Define the run method in this subclass. Doing so will override the run method of class Thread.

An instance of the subclass can then be allocated in memory and started.

This is an example of a thread that simply writes whatever input it receives when it's created:

class Printer extends Thread { String toPrint; Printer(String toPrint) { this.toPrint = toPrint; } public void run() { System.out.println(toPrint); } }

The following code would then create a thread instance and start it running to print the input it received as an argument:

Printer p = new Printer ("I am a subclass of class Thread"); p.start();

Implement the Runnable interface

Declare a class that implements the Runnable interface. Define the run method in this class. This class now implements the run method.

An instance of this class can then be allocated in memory, passed as an argument when creating the Thread, and started.

This is the same example but instead of subclassing class Thread it implements the Runnable interface:

class Printer implements Runnable { String toPrint; Printer(String toPrint) { this.toPrint = toPrint; } public void run() { System.out.println(toPrint); } }

The following code would then create a thread and start it running:

Printer p = new Printer ("I am implementing the Runnable interface"); new Thread(p).start();

Assignment Description

Create a Java applet that implements a GUI with buttons and a text area.

When a button is first pressed the Applet will start a new thread that prints a text in the text area, e.g. "Thread one is running".

Next time the button is pressed the thread should stop and wait for it to be pressed once again... and so on. Two threads, and consequently two buttons, should be used.

The first thread should extend Java's class Thread, and the second should implement Java's Runnable interface.

My Solution, Assignment Files

Since this is a simple example to demonstrate the use of applets and threads it might not be of much real use. If, however, you need to use and/or alter some snippets or all of the code, feel free to do so.

Try the Threaded Java Applet

«  Previous Next  »