Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
LEARNING OBJECTIVES
q
q q
Write thread by extending the Thread class. Write threads by implementing the Runnable interface in cases of multiple inheritance.
Understand the life cycle of thread states and set thread priorities.
22
TOPICS
q q q
Threads Concept Creating Threads by Extending the Thread class Creating Threads by Implementing the Runnable Interface Controlling Threads and Thread Status Thread Groups Synchronization
q q q
33
Threads concept
Multiple threads on multiple CPUs
Ta hd r 1 e Ta hd r 2 e Ta hd r 3 e
Ta hd r 1 e Ta hd r 2 e Ta hd r 3 e
44
// Custom thread class // Client class public class CustomThread extends Thread public class Client { { ... ... public CustomThread(...) public someMethod() { { ... ... } // Create a thread CustomThread thread = new CustomThread(.. // Override the run method in Thread public void run() // Start a thread { thread.start(); // Tell system how to run custom thread ... ... } } ... ... } }
55
The first thread prints the letter a 100 times. The second thread prints the letter b 100 times. The third thread prints the integers 1 through 100.
66
// Implement the run method in Runnable public void run() { // Tell system how to run custom thread ... } ... }
77
The first thread prints the letter a 100 times. The second thread prints the letter b 100 times. The third thread prints the integers 1 through 100.
88
void run() Invoked by the Java runtime system to execute the thread. You must override this method and provide the code you want your thread to execute. void start() Starts the thread, which causes the run() method to be invoked. Called by the runnable object in the client class. static void sleep(long millis) throws InterruptedException Puts the runnable object to sleep for a specified time in milliseconds.
99
void stop() Stops the thread. void suspend() Suspends the thread. Use the resume() method to
resume.
q
method.
1010
Thread Priority
q
1111
Thread States
ready
stop
finished
run
blocked
1212
Thread Groups
q
1313
Thread Groups
q
To find out how many threads in a group are currently running, use the activeCount() method:
System.out.println("The number of + runnable threads in the group + g.activeCount());
1414
Synchronization
A shared resource may be corrupted if it is accessed simultaneously by multiple threads. For example, two unsynchronized threads accessing the same bank account causes conflict.
Step 1 2 3 4 balance 0 0 1 1 thread[ i]
newBalance = bank.getBalance() + 1; newBalance = bank.getBalance() + 1; bank.setBalance( newBalance); bank.setBalance( newBalance);
thread[j]
1515
Objective: create and launch 100 threads, each of which adds a penny to a piggy bank. Assume that the piggy bank is initially empty.
Object
-char token +getToken +setToken +paintComponet +mouseClicked PiggyBankWithoutSync -PiggyBank bank -Thread[] thread +main
1
Thread
-char token +getToken +setToken 100 +paintComponet +mouseClicked AddAPennyThread +run()
1
Object
-char token +getToken +setToken +paintComponet +mouseClicked PiggyBank -balance +getBalance +setBalance
1616
1717