Help: Using Threads to Maintain Application Liveness


Help is available for each task, or you can go straight to the solution source code.

Task 1

Modify the URLFetchPanel class to be Runnable. Add a variable to the class to hold a Thread.
Add the clause "implements Runnable" to the class signature. Add a varible of type Thread whose name is "fetchThread".

Task 2

Modify the action method to create a new Thread, store it in the thread variable and start it. Remove the call to getURL.
Remove the call to getURL. Replace it with a call to create a new thread with the variable this as its argument. Assign the result to the variable fetchThread. Then call its start method.

Task 3

Create a run method to do the work of the getURL method. It will replace the getURL method.
The run method will do exactly what the getURL method does, and even has the same signature, so just rename the getURL method to "run".

Task 4

Compile and run the applet. To test the URL fetch, type some URL into the text field and press the "Get URL" button. If you are running the applet in a browser, you will probably only be able to fetch a URL from the same site where the applet is loaded from.
If the applet does not seem to be working, examine the Java ouput window for security violations. If the applet is run from the appletViewer, the security restrictions can be loosened to allow a fetch from any web site.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.