0% found this document useful (0 votes)
36 views

Muhammad Multithreading

Multithreading and concurrency allow a program to handle multiple tasks simultaneously. Threads are lightweight processes that can run concurrently within a program. Java supports multithreading through its Thread class and Runnable interface. Synchronization is needed to prevent race conditions when multiple threads access shared resources concurrently. The synchronized keyword and synchronized blocks are used to provide exclusive access to a shared object or block of code.

Uploaded by

terencezhao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Muhammad Multithreading

Multithreading and concurrency allow a program to handle multiple tasks simultaneously. Threads are lightweight processes that can run concurrently within a program. Java supports multithreading through its Thread class and Runnable interface. Synchronization is needed to prevent race conditions when multiple threads access shared resources concurrently. The synchronized keyword and synchronized blocks are used to provide exclusive access to a shared object or block of code.

Uploaded by

terencezhao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Multithreading / Concurrency

Thread
Thread: single sequential flow of control within a
program
Single-threaded program can handle one task at any
time.
Multitasking allows single processor to run several
concurrent threads.
Most modern operating systems support multitasking.

Threads Concept
Multiple
threads on
multiple
CPUs
Multiple
threads
sharing a
single CPU

Thread 1
Thread 2
Thread 3

T hread 1
T hread 2
T hread 3

Threads in Java
Creating threads in Java:
Extend java.lang.Thread class
OR
Implement java.lang.Runnable interface

Threads in Java
Creating threads in Java:
Extend java.lang.Thread class
run() method must be overridden (similar to main method of
sequential program)
run() is called when execution of the thread begins
A thread terminates when run() returns
start() method invokes run()
Calling run() does not create a new thread

Implement java.lang.Runnable interface


5

Threads in Java
Creating threads in Java:
Extend java.lang.Thread class
Implement java.lang.Runnable interface
Single method: public void run()
Thread class implements Runnable.

Thread termination
A thread becomes Not Runnable when one of these
events occurs:
Its sleep method is invoked.
The thread calls the wait method to wait for a specific
condition to be satisifed.
The thread is blocking on I/O.

Creating Tasks and


Threads
java.lang.Runnable

TaskClass

// Custom task class


public class TaskClass implements Runnable {
...
public TaskClass(...) {
...
}

// Client class
public class Client {
...
public void someMethod() {
...
// Create an instance of TaskClass
TaskClass task = new TaskClass(...);
// Create a thread
Thread thread = new Thread(task);

// Implement the run method in Runnable


public void run() {
// Tell system how to run custom thread
...
}
...
}

// Start a thread
thread.start();
...
}
...
}

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
+isAlive(): boolean

Starts the thread that causes the run() method to be invoked by the JVM.
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.

The Static yield() Method


You can use the yield() method to temporarily
release time for other threads.
public void run() {

for (int i = 1; i <= lastNum; i++) {


System.out.print(" " + i);
Thread.yield();
}

Every time a number is printed, the print100


thread is yielded.

10

The Static sleep(milliseconds)


Method
The
sleep(long mills) method puts the thread to sleep for the specified time in milliseconds.
public void run() {
for (int i = 1; i <= lastNum; i++) {
System.out.print(" " + i);
try {
if (i >= 50) Thread.sleep(1);
}
catch (InterruptedException ex) {
}
}

Every time a number (>= 50) is printed, the print100 thread is put to sleep for 1 millisecond.

11

The join() Method


You can use the join() method to force one thread to
wait for another thread to finish

The numbers after 50 are printed after thread printA is finished.


12

Thread States
A thread can be in one of five states:
New, Ready, Running, Blocked, or
Finished.
yield(), or
time out
Thread created

start()
New

Ready
Target
finished

Running
run()

run() returns

join()

interrupt()

Wait for target


to finish

Finished
sleep()
wait()
Wait for time
out

Wait to be
notified

Time out

notify() or
notifyAll()

Blocked
Interrupted()

13

Thread methods
isAlive()
method used to find out the state of a thread.
returns true: thread is in the Ready, Blocked, or
Running state
returns false: thread is new and has not started
or if it is finished.
interrupt()
f a thread is currently in the Ready or Running
state, its interrupted flag is set; if a thread is
currently blocked, it is awakened and enters the
Ready state, and an java.io.InterruptedException
is thrown.
The isInterrupt() method tests whether the thread
is interrupted.
14

The deprecated stop(), suspend(), and resume()


Methods
NOTE: The Thread class also contains the stop(),
suspend(), and resume() methods. As of Java 2, these
methods are deprecated (or outdated) because they
are known to be inherently unsafe.

15

Thread Priority
Each thread is assigned a default priority
of Thread.NORM_PRIORITY (constant of 5).
You can reset the priority using
setPriority(int priority).
Some constants for priorities include
Thread.MIN_PRIORITY Thread.MAX_PRIORITY
Thread.NORM_PRIORITY
By default, a thread has the priority level of
the thread that created it.

16

Thread Scheduling
An operating systems thread scheduler
determines which thread runs next.
Most operating systems use timeslicing for
threads of equal priority.
Preemptive scheduling: when a thread of
higher priority enters the running state, it
preempts the current thread.
Starvation: Higher-priority threads can
postpone (possible forever) the execution
of lower-priority threads.
17

Thread Pools
Starting a new thread for each task could limit throughput and
cause poor performance.
A thread pool is ideal to manage the number of tasks executing
concurrently.
Executor interface for executing Runnable objects in a thread pool
ExecutorService is a subinterface of Executor.
interface
java.util.concurrent.Executor
+execute(Runnable object): void

Executes the runnable task.

\
interface
java.util.concurrent.ExecutorService
+shutdown(): void

Shuts down the executor, but allows the tasks in the executor to
complete. Once shutdown, it cannot accept new tasks.

+shutdownNow(): List<Runnable>

+isShutdown(): boolean

Shuts down the executor immediately even though there are


unfinished threads in the pool. Returns a list of unfinished
tasks.
Returns true if the executor has been shutdown.

+isTerminated(): boolean

Returns true if all tasks in the pool are terminated.

18

Creating Executors
To create an Executor object, use the static methods in the Executors
class.
java.util.concurrent.Executors
+newFixedThreadPool(numberOfThreads: Creates a thread pool with a fixed number of threads executing
int): ExecutorService
concurrently. A thread may be reused to execute another task
after its current task is finished.
+newCachedThreadPool():
Creates a thread pool that creates new threads as needed, but
ExecutorService
will reuse previously constructed threads when they are
available.

19

Thread Synchronization

A shared resource may be corrupted if it is


accessed simultaneously by multiple threads.
Example: two unsynchronized threads accessing
the same bank account may cause conflict.
Step

balance

thread[i]

1
2
3
4

0
0
1
1

newBalance = bank.getBalance() + 1;

thread[j]

newBalance = bank.getBalance() + 1;
bank.setBalance(newBalance);
bank.setBalance(newBalance);

20

Race Condition
What, then, caused the error in the example? Here is a possible
scenario:
Step

balance

Task 1

1
2
3
4

0
0
1
1

newBalance = balance + 1;

Task 2

newBalance = balance + 1;
balance = newBalance;
balance = newBalance;

);

Effect: Task 1 did nothing (in Step 4 Task 2 overrides the result)
Problem: Task 1 and Task 2 are accessing a common resource in a way
that causes conflict.
Known as a race condition in multithreaded programs.
A thread-safe class does not cause a race condition in the presence of
multiple threads.

21

synchronized
Problem: race conditions
Solution: give exclusive access to one thread at a time
to code that manipulates a shared object.
Synchronization keeps other threads waiting until the
object is available.
The synchronized keyword synchronizes the method so
that only one thread can access the method at a time.

22

Synchronizing Instance Methods and Static Methods

A synchronized method acquires a lock before it


executes.
Instance method: the lock is on the object for which it
was invoked.
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, 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.
23

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
token
Acquire a-char
lock
on the object account
+getToken
-char token +setToken
+paintComponet
+getToken
Execute
the deposit method
+mouseClicked
+setToken
+paintComponet
-char
token
+mouseClicked
+getToken
Release the lock
+setToken
+paintComponet
-char token
+mouseClicked
+getToken
+setToken
+paintComponet
+mouseClicked

Task 2
-char token
+getToken
+setToken
+paintComponet
+mouseClicked

Wait to acquire the lock


-char token
+getToken
Acqurie a lock
+setToken
+paintComponet
-char
token
+mouseClicked

on the object account

+getToken
Execute the deposit method
+setToken
+paintComponet
-char
token
+mouseClicked
+getToken
Release the lock
+setToken
+paintComponet

24

Synchronizing Statements
Invoking a synchronized instance method of an object
acquires a lock on the object.
Invoking a synchronized static method of a class acquires
a lock on the class.
A synchronized block can be used to acquire a lock on any
object, not just this object, when executing a block of code.
synchronized (expr) {
statements;
}

expr must evaluate to an object reference.


If the object is already locked by another thread, the
thread is blocked until the lock is released.
When a lock is obtained on the object, the statements in
the synchronized block are executed, and then the lock is
released.
25

Synchronizing Statements vs. Methods


Any synchronized instance method can be converted
into a synchronized statement. Suppose that the
following is a synchronized instance method:

public synchronized void xMethod() {


// method body
}

This method is equivalent to


public void xMethod() {
synchronized (this) {
// method body
}
}

26

Synchronization

Using Locks

A synchronized instance method implicitly acquires a


lock on the instance before it executes the method.
You can use locks explicitly to obtain more control for
coordinating threads.
A lock is an instance of the Lock interface, which
declares the methods for acquiring and releasing locks.
newCondition() method creates Condition objects,
which can be used for thread communication.
interface
java.util.concurrent.locks.Lock
+lock(): void

Acquires the lock.

+unlock(): void

Releases the lock.

+newCondition(): Condition

Returns a new Condition instance that is bound to this


Lock instance.

java.util.concurrent.locks.ReentrantLock
+ReentrantLock()

Same as ReentrantLock(false).

+ReentrantLock(fair: boolean)

Creates a lock with the given fairness policy. When the


fairness is true, the longest-waiting thread will get the
lock. Otherwise, there is no particular access order.

27

Fairness

Policy

ReentrantLock:concrete implementation of
Lock for creating mutually exclusive locks.
Create a lock with the specified fairness policy.
True fairness policies guarantee the longestwait thread to obtain the lock first.
False fairness policies grant a lock to a waiting
thread without any access order.

28

Cooperation Among Threads


Conditions can be used for communication among threads.
A thread can specify what to do under a certain condition.
newCondition() method of Lock object.
Condition methods:

await() current thread waits until the condition is signaled


signal() wakes up a waiting thread
signalAll() wakes all waiting threads

interface
java.util.concurrent.Condition
+await(): void

Causes the current thread to wait until the condition is signaled.

+signal(): void

Wakes up one waiting thread.

+signalAll(): Condition

Wakes up all waiting threads.

29

Cooperation Among Threads


Lock with a condition to synchronize operations: newDeposit
If the balance is less than the amount to be withdrawn, the
withdraw task will wait for the newDeposit condition.
When the deposit task adds money to the account, the task
signals the waiting withdraw task to try again.
Interaction between the two tasks:
Deposit Task

Withdraw Task
-char token

-char token

+getToken
+setToken
-char token
+paintComponet
while
(balance < withdrawAmount)
+mouseClicked
+getToken
newDeposit.await();
+setToken
+paintComponet
+mouseClicked

+getToken
+setToken
-char token
+paintComponet
balance += depositAmount
+mouseClicked
+getToken
+setToken
+paintComponet
-char
token
newDeposit.signalAll();
+mouseClicked

lock.lock();

balance -= withdrawAmount
-char token

lock.unlock();
+getToken
+setToken

lock.lock();

+getToken
+setToken
lock.unlock();
+paintComponet
+mouseClicked
-char token

30

wait(), notify(), and notifyAll()


Use the wait(), notify(), and notifyAll() methods to
facilitate communication among threads.
The wait(), notify(), and notifyAll() methods must be
called in a synchronized method or a synchronized
block on the calling object of these methods.
Otherwise, an IllegalMonitorStateException would
occur.
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.
31

Deadlock
Sometimes two or more threads need to acquire the locks on
several shared objects.
This could cause deadlock, in which each thread has the lock on
one of the objects and is waiting for the lock on the other object.
In the figure below, the two threads wait for each other to release
the in order to get a lock, and neither can continue to run.
Step
1
2
3
4
5
6

Thread 2

Thread 1
synchronized(object1){

//dosomethinghere

synchronized(object2){

//dosomethinghere
}
}

Wait for Thread 2 to


release the lock on object2

synchronized(object2){

//dosomethinghere

synchronized(object1){
//dosomethinghere
}
}

Wait for Thread 1 to


release the lock on object1

32

Preventing Deadlock

Deadlock can be easily avoided by resource ordering.


With this technique, assign an order on all the objects
whose locks must be acquired and ensure that the
locks are acquired in that order.
How does this prevent deadlock in the previous
example?

33

You might also like