Lab - Exp - 12 THREADS - Kabigting
Lab - Exp - 12 THREADS - Kabigting
EXERCISE
12
Using Single and Multi Threads
MIGUEL TEREL KABIGTING
CSIT 26
SEPTEMBER 25, 2020
I.
OBJECTIVES:
Psychomotor:
a) construct a program using thread
b) compile and debug the error of the program
Affective
a) appreciate the concept behind this experiment
II.
BACKGROUND INFORMATION:
A thread is a program's path of execution. Threads allow us to execute two or more sections of a program at the same time. The first method of creating a thread is to simply extend
from the Thread class.
When a thread is created, it must be permanently bound to an object with a run() method. When the thread is started, it will invoke the object's run() method. More specifically, the
object must implement the Runnable interface.
III.
EXPERIMENTAL PROCEDURE:
1.
Using AWT or Swing, create a simple banner that prints out the string entered by the
user. The string is displayed continuously and your program should give the illusion that
the string is moving in the leftward direction. To make sure that the movement is not too fast, you should use the sleep method of the Thread class. Say
that the input string is "Miguel Kabigting!". Here is a sample output.
Source code:
Output:
2. Inherit a class from Thread and override the run( ) method. Inside run( ), print a message, and then call sleep( ). Repeat this three times, then return from run( ).
Put a start-up message in the constructor and override finalize( ) to print a shut-down message. Make a separate thread class that calls System.gc( ) and
System.runFinalization( ) inside run( ), printing a message as it does so. Make several thread objects of both types and run them to see what happens.
Hint:
The garbage collector works automatically, whether you request it to or not. However, it is possible to make a garbage collector request, by invoking the System.gc()
method. When garbage collector starts its work, you may have a short pause in the application's execution. This is the reason why you should only invoke the
System.gc() method at the right time, such as before performing a huge task or when the application is idle, waiting for user's input.
Before an object is garbage collected, the Java runtime system gives the object a chance to clean up after itself. This step is known as finalization and is
acheived through a call to the object's finalize() method. The object should override the finalize() method to perform any final cleanup tasks such as
freeing system resources like files and sockets. You can force object finalization to occur by calling System's runFinalization() method. This method calls
the finalize() methods on all objects that are waiting to be garbage collected.
Source code:
Output: