0% found this document useful (0 votes)
4 views27 pages

Multithreading Concepts and Performance

Uploaded by

Vikas NS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

Multithreading Concepts and Performance

Uploaded by

Vikas NS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

① Why Multithreading (solved with concurrency)

=>
Responewnes =
· UI + NetworkCall
Multiple used
accessing
·

service

wh muca
Performan
=>

② One core can handle One Thread


>
- Multi tasking / Concurrency
=> One love content switches
between and illusion
an
gives
of parallelisis

Process

content-when anaplatonrun a
n

· - opened
P
by

meta data

Processes solated
-
are
from one another
. Multi
4 threaded Procus

.
5 Stack
-

Region
and
in
memory where local variables
function invocation are
present
6
. Instruction Pointes
a
Address of nextInstruction
-

7 .
Content Switch
As CPU core process
a
Single thread can
-

at time ,
only one a

it neede to switch content to run

another thread
-

Stop Inread 1
-
Schedule thread 1 out
- Schedule Aread 2 in
-
Start thread 2
This lack
-
is expensive
-
content Switching also involves
thread 1's data
-
storing
-
Ihread
Restoring 2's data

: Threads VIS Processes

① When
When
security
two
and volation
threads don't
is
share
preffered
② same
data

① Threads are lightweight


blu threads
in same
Content switches

faster
process is

9 .

I
read
scheduling

·
UI threade go
unresponse

② Problem
Too
many
-

shorter jobs
-Longer Sa .

10 Thread sleep doesn't consume CPU resources


schedule my that
.
-

It convey to the it
doesn't need to be picked until that time
.
Thread Creation
Termination


Interrupted
>
- thread
throus
is sleeping or waiting
interrup exception
.

-> Else developer needs to check


it interrupted & action
if take

② Daemon Thread
>
-

They terminate when the main


Itread & all threads are completed
user
defined
>
-

leage>
When code (
running
-

from
have
lib) where we don't
control and caut
check interrupted
if don't
When
> we want
background
-

tasks to block the


application terminations
-

Lie-task that needs


to run only
when the
6
application
is
running
en .

Garbage Collection
Join
#read
① Say we
spin up
5 threads to
5 problems
compate solution
for
② In the main thread we have to constantly
check
This is
if the
resource
computation
influsive
Itread has completed

③ Instead main thread can jon the

compulation thread and blocked and


unblocked when
get &

then
tread
to
computation
is compled
=> main thread computation thread,

S
123

join
3
jar S 33
The join() method may also
When we invoke the join() method on a return if the referenced This means
thread, the calling thread goes into a thread was interrupted. In that when a
waiting state. It remains in a waiting this case, the method throws thread t1 calls
state until the referenced thread an InterruptedException. [Link](), then
terminates. Finally, if the referenced all changes
thread was already done by t2
terminated or hasn't been are visible in
started, the call to join() t1 on return.
method returns immediately.
Measuring Performance

① How to Reduce Latering ?


Task A takes T time
>
-
If
>
-
Brak the task intoa independent
blocks :

>
-
Now latency .
>
-

T/u
= This i themilakal as
it needs a cores
① small tasks perform
wont when compared
to
single thread

focessing
Image case

① Given an
image of M x N picces
Pisces
① Identify Greg
② Wite Purple color to that
pucel
-
Observation
① Image MXN pixel should be

big enough
When
to the
benifit of Mutti thread
② dividing the problem into

subproblem
until
↑ Performance increase

n < = cores

& n < = Loves +


/ logical
cora .
Put
Through

ebserver!
How

stayinganyquestcan you
Pre and
threshold

#
proach Divide each
and reduce
Request
intoa
single
of Request
Doubtasks
the
latencywill
② Wit the 3 request be processed
request Takes las time
quickly as each

Overhead -

③ Downside
-

intaa b c

-Baking
>
-

proch
A ① Let each talk run in its own

thread
② Advantage
No overhead.
I
Pooling
Thread
>
-
No need
Runnable
to create a thread
for each

un
In
Data Storage in M T
.

a

=> Smethod Frame


② Leap
>
-
Itreads
in same
proces
Resource sharing
Wes
① TextEditor App

#
L
② Task Gueve

#T
I

TEnDu
Processing
thread

guareos, M
1

Connections

· -

thread E
=>
lit
~
her are

pro bl e mswi t h
shared

O
Kit li


i--
i+ +

it+& i-- are not atomic operations


the
O get
increment it
value
of i

write it back to
③ memory

=> When two llireads are


running the
make
of I could be in correct
Solutions
① Use of Synchronized keyword
at method level

=> Synchronized is
applied at
object /
level In this case at that clas
leve

read i
=> Even
though In A
accessing
onlya
method all the methods
closs' are now locked
and can't be accessed by
other thread .
any
b) Using Synchronized Blocks .

=> Using this mimiss the previous


approach
=> But this can be replaced with
any
dhe object
Idea
=>
is to reduce the

synchronizedchunks see
to

>
-
When "this" or "obj" is
being
accessed one thread
other thread
by can
,
access it.
no
Atomic
-
Operations

1 .

Reference Assignments
Object a = new
Obj));
Object b = a
,

string is
① object,
As an

> . refence
- update
reading from reference
·

are atomie

.
2
Assignment to
double
&
primitive
(64 bit)
types except for
long
=> int >
float
-

>
short > Char
-
-

>
-

byte >
-
boolean

volatile
=>
keyword
counter
makes
X read
of
synchroed .
-
Visibility Problem .

① If both threads the

on twodifferentor a

GReader
be cached in its
own core cache
eac % . Lacke updates are

feredand
is
he

memory
.
ii. This means when

I maineach main thread updates

o
the value, Reader might
eit right awayaa
ordering
The order execution revised
of could be
is true
when
ready
-

number
meaning ,

could still be .
0

Solution-Volatile
Keyword
① Ensures is
update made by I thread
other
visible to
rightcachay and
is, it prevent
straight
away updates memory
.
# Volatile onlysolves vnibility problem
the
not
synchronization

&
Zo
Sitt
i ++

=> In the caseI should be 2

volatile
but evenwith
Leyword
this is not the, we need
explicit
synchronyatos
Patie
word

① Problems could crise when threads run on

Each core
2different
coves ·
has it
ona

② Also , the code could be recordered


I
·
1I
int
library [][
. ]; #
-
nt[][] =
Libering

int n [10] 20 ,
1
+

int * PAn =
( * int) nallos NE
.

int # & 1111

(int) ()

(pt)() III
an
and

You might also like