1 - 2
Fundamentals of Java Programming Lab 15.3.2
Copyright
2003, Cisco Systems, Inc.
Lab 15.3.2 Threads and Priorities
Estimated Time: 30 minutes
Learning Objective:
• In this lab activity, the student will create multithreading solutions using the Thread class to run
several threads that display the same information.
Description/Scenario:
• In the Java language, the Virtual CPU that is created to manage a thread is an instance
of java.lang.Thread class. In other words, a Thread object is a virtual CPU that runs code
and uses data. More than one Thread object can share the code and data.
• Any class whose instances are intended to be executed by a thread should implement
the Runnable interface. The class must define a method of no arguments called run().
A class that implements Runnable can run without sub classing Thread by instantiating
a Thread instance and passing itself in as the target.
• One way to create a thread is to create it in a class and pass it a reference to an object
that implements Runnable. A second way to create a thread is to extend from Thread.
• A thread starts with the start() method. This calls the run() method of the thread or
the Object implementing Runnable.
• The run() method can be that of an object that implements the Runnable interface or a
Thread object that overrides the run() method. A programmer cannot control the
schedule for a thread, but can assign a priority to a thread.. The JVM schedules threads
of same priority. Other method calls can be used to yield to a thread of same or higher
priority. Use the getPriority() method to determine the current priority of the thread.
Use the setPriority() method to set the priority of the thread. The priority is an
integer value. The Thread class includes the following constants:
• Thread.MIN_PRIORITY
• Thread.NORM_PRIORITY
• Thread.MAX_PRIORITY
• This lab covers creation of a Thread Class and instantiation of three different threads.
• The student will set Thread priorities and observe the results
• This lab covers Thread priorities and the affects priority has on Thread processes.
File Management:
Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New Project
window and in the Look in: list box select c:\. Now, double click the javacourse folder listed in
the text window and a different New Project window opens with javacourse in the Look in: list
box. Now, double click the chap15 folder listed in the text window and a different New Project
window opens with chap15 in the Look in: list box. Type lab15.3.2 in the File name text box
and click on Create to create a lab15.3.2subfolder in the chap15 folder.
Import the JBANK classes from lab15.3.2.
Tasks:
Step 1 Create IntThread class
a. Create a class named IntThread that extends Thread and has a constructor that
accepts an int as an argument.
b. Add
a
run() method with a for loop that displays the int argument 15 times.
Step 2 Create TestThread class
a. Create a class named TestThread that contains a main method.
2 - 2
Fundamentals of Java Programming Lab 15.3.2
Copyright
2003, Cisco Systems, Inc.
b. Have the main method instantiate three Threads of IntThread class and pass the ints
1, 2, and 3. Use the start() method to start each Thread.
c. Run the TestThread class and observe which int displays first.
Step 3 Create TestThread2 class
a. Create a class named TestThread2 that contains a main method.
b. Have the main method instantiate three Threads of IntThread class and pass the ints
1, 2, and 3.
c. Add
setPriority() methods to set the priorities for each Thread as follows:
one.setPriority(Thread.MIN_PRIORITY);
two.setPriority(Thread.MIN_PRIORITY + 1);
three.setPriority(Thread.MIN_PRIORITY + 2);
d. Run the TestThread2 class and observe which int displays first.