15_Multithreading
15_Multithreading
1
Threads Concept
Multiple Thread 1
threads on
Thread 2
multiple
Thread 3
CPUs
Multiple Thread 1
threads
Thread 2
sharing a
Thread 3
single CPU
2
Using the Runnable Interface to Create
and Launch Threads
• Objective: Define a Task, create objects of Task and
run in threads:
• MyTask prints a text and sleeps for a while
Interface Runnable {
void run();
}
class MyTask implements Runnable {
}
3
Creating Tasks and Threads
4
The Thread Class
«interface»
java.lang.Runnable
java.lang.Thread
+Thread() Creates a default thread.
+Thread(task: Runnable) Creates a thread for a specified task.
+start(): void Starts the thread that causes the run() method to be invoked by the JVM.
+isAlive(): boolean Tests whether the thread is currently running.
+setPriority(p: int): void Sets priority p (ranging from 1 to 10) for this thread.
+join(): void Waits for this thread to finish.
+sleep(millis: long): void Puts the runnable object to sleep for a specified time in milliseconds.
+yield(): void Causes this thread to temporarily pause and allow other threads to execute.
+interrupt(): void Interrupts this thread.
5
The Static yield() Method
You can use the yield() method to temporarily release time
for other threads. For example, suppose you modify the
code in Lines 53-57 in TaskThreadDemo.java as follows:
6
The Static sleep(milliseconds) Method
The sleep(long mills) method puts the thread to sleep for the
specified time in milliseconds. For example, suppose you modify the
code in Lines 53-57 in TaskThreadDemo.java as follows:
Every time a number (>= 50) is printed, the print100 thread is put to
sleep for 1 millisecond.
7
The join() Method
You can use the join() method to force one thread to wait for another
thread to finish. For example, suppose you modify the code in Lines
53-57 in TaskThreadDemo.java as follows:
yield(), or Running
time out run() returns
Thread created start()
New Ready run() join() Finished
interrupt() sleep()
Target wait()
finished
9
isAlive(), interrupt(), and isInterrupted()
The isAlive() method is used to find out the state of a
thread. It returns true if a thread is in the Ready, Blocked,
or Running state; it returns false if a thread is new and has
not started or if it is finished.
11
Thread Synchronization
1 0 newBalance = bank.getBalance() + 1;
2 0 newBalance = bank.getBalance() + 1;
3 1 bank.setBalance(newBalance);
4 1 bank.setBalance(newBalance);
12
Example: Showing Resource Conflict
• Objective: Write a program that demonstrates the problem of
resource conflict. Suppose that you create and launch one
hundred threads, each of which adds a penny to an account.
Assume that the account is initially empty.
java.lang.Runnable
-char token
100 1 1 1
AddAPennyTask
+getToken AccountWithoutSync Account
+setToken
+paintComponet -bank: Account -balance: int
+mouseClicked
+run(): void -thread: Thread[]
+getBalance(): int
+deposit(amount: int): void
+main(args: String[]): void
AccountWithoutSync
Run
13
Race Condition
What, then, caused the error in the example? Here is a possible scenario:
1 0 newBalance = balance + 1;
2 0 newBalance = balance + 1;
3 1 balance = newBalance;
4 1 balance = newBalance;
);
15
Synchronizing Instance Methods and Static
Methods
A synchronized method acquires a lock before it executes.
In the case of an instance method, the lock is on the object
for which the method was invoked. In the case of a static
method, the lock is on the class. If one thread invokes a
synchronized instance method (respectively, static method)
on an object, the lock of that object (respectively, class) is
acquired first, then the method is executed, and finally the
lock is released. Another thread invoking the same method
of that object (respectively, class) is blocked until the lock is
released.
16
Synchronizing Instance Methods and Static
Methods
With the deposit method synchronized, the preceding scenario
cannot happen. If Task 2 starts to enter the method, and Task 1 is
already in the method, Task 2 is blocked until Task 1 finishes the
method.
Task 1 Task 2
Acquire a-char
locktoken
on the object account -char token
+getToken +getToken
-char token +setToken +setToken
+paintComponet +paintComponet
Execute
+getToken the deposit method
+mouseClicked +mouseClicked
+setToken
+paintComponet Wait to acquire the lock
-char token
+mouseClicked
-char token
+getToken Release the lock
+setToken
+getToken
+paintComponet
-char token Acqurie a lock on the object account
+setToken
+mouseClicked
+paintComponet
+getToken -char token
+mouseClicked
+setToken
+paintComponet Execute the deposit method
+getToken
+mouseClicked +setToken
+paintComponet
-char token
+mouseClicked
+getToken Release the lock
+setToken
+paintComponet
17
Synchronizing Statements
Invoking a synchronized instance method of an object acquires a lock
on the object, and invoking a synchronized static method of a class
acquires a lock on the class. A synchronized statement can be used to
acquire a lock on any object, not just this object, when executing a
block of the code in a method. This block is referred to as a
synchronized block. The general form of a synchronized statement is
as follows:
synchronized (expr) {
statements;
}
18
Synchronizing Statements vs. Methods
Any synchronized instance method can be converted into a
synchronized statement. Suppose that the following is a synchronized
instance method:
19
wait(), notify(), and notifyAll()
Use the wait(), notify(), and notifyAll() methods to facilitate
communication among threads.
The wait() method lets the thread wait until some condition occurs.
When it occurs, you can use the notify() or notifyAll() methods to
notify the waiting threads to resume normal execution. The
notifyAll() method wakes up all waiting threads, while notify() picks
up only one thread from a waiting queue.
20