0% found this document useful (0 votes)
17 views14 pages

Multi Threading - 21-09-23

This document discusses concepts related to multi-threading including: 1. What threads are and how to create multi-threaded code. Threads allow parallel execution of tasks. 2. The difference between concurrency and parallelism. Concurrency is the illusion of multiple tasks happening at once using time slicing, while parallelism is true simultaneous execution using multiple cores. 3. Steps for writing multi-threaded code in Java including implementing the Runnable interface, creating Thread objects, and calling the start() method.

Uploaded by

fazil
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)
17 views14 pages

Multi Threading - 21-09-23

This document discusses concepts related to multi-threading including: 1. What threads are and how to create multi-threaded code. Threads allow parallel execution of tasks. 2. The difference between concurrency and parallelism. Concurrency is the illusion of multiple tasks happening at once using time slicing, while parallelism is true simultaneous execution using multiple cores. 3. Steps for writing multi-threaded code in Java including implementing the Runnable interface, creating Thread objects, and calling the start() method.

Uploaded by

fazil
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
You are on page 1/ 14

Concept of multi threading

what are threads

f concumney
Parallelism

How to create thread

multi traded code


Steps for writing
Can threads them data if
then how
yes

Implementations in
Jang

Swe
lad Jie
mi I executable
file
download

I
run
on

server

PygramI Something that can be equated to cause


a particular action
tweet Program in equation

I
CPU processor
p executes on
a Store data RAM
for process
scalercom youtube.cominterviewbillon
egg
I
42 B

I
Chrome

Tome

f
cores brain
Thread I one unit of an execution

I
Icord a core enecutes threads

4 Every thread needs core to execute


4 Every process needs to have atleast t thread

Mars Main

psume ss
sourceHelloworld

3
3
Jointure
Qais Main
psumC

3
doll no matter
how
manyclans
3 methods we have
all thecode
men in a
printed single
Hood
Mainfod
3

Concurrency Parallelism

Pentium I core

1
DualCore Core2dm 2 core

te
iz is it AMD mi me 418116132
cons

illusion of parallel work


Concumneyt
Content Switching

B
atBat it i

A IE
WMP
O A lot
B
O B A lot
A O C lot
Barbquenation

O
O 8

Kyllo O

4 Parallelism

cons for parallelism 2


minim no
of

In

to
Parallelism is truly parallelism

MIF
9
ConcumanyeParalleling

Cpu cando parallelism can it also do concurrency


If a
3 Yes

If a giftcando conceumery can it also do parallelism


3 NI

How to write multi threaded code

And
Dont think in terms of threads

them in terms of what tasks youwant to run parallely

Create a multi threaded program which prints Hello world


ALI thread main thread
from a separate

amor
ETE
SI Cars for hasnt classname
should be a
class HelloworldPrinter
noun doinga very
33 I
Ipunentrunniurfant
Printer Runnable
gars Helloworld implements
t
has amethod called nunc

Need to implement nunc and this


method will egecute the

fast st
Implement
class HelloworldPrinter implements Runnable

public void nail


South Hello world
3
3
Task defer being done a
setosy

SI Threadcreation
a create the object the taste clans

by create a thread wring the object


c thread Start
run17 Start
te te
contains the
p spawns a new
actual taste thread
that we want 4 callsthenunc on
to execute that new thread

A
executes
onthe
samethread

main

few main
way

Ito runes
to popmain

132
Ift
172 132

Any Priutnumberltop y
print eachnumber
on a
diff thread

for the task


attribute class

valve via construct


pass

use this attribute inside run meth


i

spay

Fit
i
Zora
I
TF present
I
b 6 a
DSA Design
development

me
Pay

1 tepeads maintaining queue


Reusability of
takes care thread creation
in
of
faecutor
Normality Frowewort
Thread
Pool
Thread a thread
1
takes ataste d
f task waits for a thread
core
t
t thread assigned
executes
t d
Cox
destroyed
t
executes
t
goes back to
threadpoor

Emmy
productfinalpt
É
Cavalry interface

t
caul a setums something

we have 2 threads each thread generates a random number


Angry
and they return that to the main thread and
main thread returns the Sum

printery
I

9
95 17
Iho
ah iz calmer ofsquare
minds E
n
initialised
with result
ecutor.s unit rancent

entire
mpfutmIuteger7randon1
Integer random2

1 debit food s
reception

value present
in future gets

a
D debit find reception

n
future getti blathered
future gettys bloody

You might also like