0% found this document useful (0 votes)
31 views5 pages

Lab - Exp - 12 THREADS - Kabigting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Lab - Exp - 12 THREADS - Kabigting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

(Object Oriented Programming)

EXERCISE

12
Using Single and Multi Threads
MIGUEL TEREL KABIGTING
CSIT 26
SEPTEMBER 25, 2020
I.
OBJECTIVES:

At the end of the experiment, students must be able to:


Cognitive
a. Learn how to create and start a Thread

b. Understand the thread life-cycle

c. Know how to set Thread priority

d. Know how to pause a Thread with Thread.sleep()

e. Understand the concept of thread interference and deadlocks.

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:

IV. QUESTION AND ANSWER:

1. Can you pass a


Thread
object to
Executor.execute
? Would such an invocation make sense?
Answer: Yes, because since the Thread can implement the Runnable interface, you can pass an instance of Thread to Executor.execute. But, if the object was
instantiated directly from the Thread, then the run method won’t do anything.

You might also like