lab 15 5 2

background image

Lab 15.5.2 Controlling Threads Using Methods of the Thread Class


Estimated Time: 30 minutes

Learning Objective:

• In this lab activity, the student will use methods of the Thread class to control threads.


Description/Scenario:

• A program launches a thread's execution by calling the thread's start() method, which calls

the run() method. After start() launches the thread, start() returns to its caller
immediately. The caller then executes concurrently with the launched thread. The start()

method throws an IllegalThreadStateException if the thread it is trying to start has already been
started.

• Threads can be placed in a blocked state to allow other threads to run or to wait for

resources to become available. The programmer has several methods of the Thread
class that can be used to place threads in a blocked state, assign threads a priority, or
notify other threads when it is done. When a blocked thread becomes runnable, it is
placed back into the appropriate runnable pool. Threads from the highest priority
nonempty pool are given CPU time.

• When a sleep() method is called in a running thread, that thread enters the sleeping

state. A sleeping thread becomes ready after the designated sleep time expires. A
sleeping thread cannot use a processor even if one is available. The static method
sleep() is called with an argument specifying how long, in milliseconds, the currently

executing thread should sleep. While a thread sleeps, it does not contend for the
processor, so other threads can execute. This can give lower-priority threads a chance to
run.

• Create an applet that creates a Thread.
• Test the Thread sleep() method.

• Create an applet and inside the applet create a Thread.
• Create and start the Thread inside the init() method of the applet.
• Inside the run() method have the Thread sleep() method pause the Thread.

• Have the paint() method draw a string that is moved across the screen by the run() method.

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.5.2 in the File name text box
and click on Create to create a lab15.5.2subfolder in the chap15 folder.


Tasks:

Step 1 Create AppletThread class

a. Create a class named AppletThread that extends Applet and implements

Runnable.

b. Create and start a Thread inside the init() method.

t = new Thread(this);

t.start();

Step 2 Add String

a. Have

the

paint() method draw a String. g.drawString("Help!",x, y);

1 - 1

Fundamentals of Java Programming Lab 15.5.2

Copyright

 2003, Cisco Systems, Inc.

background image

b. Have

the

run() method move the String down the window in a diagonal line from

the left side to the right. When it reaches the right side, the String moves down and to
the left.

c. Have the String change color when the direction changes.
d. Have

a

try block inside the run() method that uses a Thread sleep() method to

pause the movement of the String.

Step 3 Run Applet

a. Run the applet in the appletviewer provided by Bluej.
b. Notice the speed of movement of the String.
c. Change the sleep time and run the applet in the appletviewer.
d. Notice the speed of movement of the String

2 - 2

Fundamentals of Java Programming Lab 15.5.2

Copyright

 2003, Cisco Systems, Inc.


Wyszukiwarka

Podobne podstrony:
Pomiar wilgotności względnej powietrza przechowalnictwo lab 15
W2K3-15-raport, WAT, SEMESTR VII, Systemy operacyjne windows, Systemy operacyjne windows, sow, W2K3-
Lab 8, 15 - rdzenie wiertnicze, SKAŁY15, Nr ˙wiczenia
lab 15
Lab 15, Notatki, FIZYKA, SEMESTR II, laborki, lab
LAB 15 OPIS I WNIOSKI, sgsp, Hydromechanika, HYDROMECHANIKA 1
lab, Lab 15, WOJSKOWA AKADEMIA TECHNICZNA
okładka-sb lab 15, Chemia Fizyczna, chemia fizyczna- laborki rozne, Rozne
IE RS lab 15 diagram
lab 15 3 2
lab 15 5 5
Lab 15 id 257443 Nieznany
IE RS lab 15 overview
Zamrażanie owiewowe przecho lab 15
lab. 15 - baron, Chemia fizyczna AGH laborki, lab 15
Pomiar wilgotności względnej powietrza przechowalnictwo lab 15
W2K3-15-raport, WAT, SEMESTR VII, Systemy operacyjne windows, Systemy operacyjne windows, sow, W2K3-
Lab 15 tez kurwa

więcej podobnych podstron