You are on page 1of 7

Mul

ti
thr
eadi
ngi
nJava
I
tist
hepr
ocessofexecut
ingmul
ti
plet
hreadssi
mul
taneousl
y.

Mul
ti
taski
ngi
saprocessofexecut
ingmul
ti
plet
askssi
mul
taneousl
y.I
t
i
sachiev
edbyusi
ngtwoway s.
1.Pr
ocessbasedmul
ti
taski
ng.
2.Thr
eadbasedmul
ti
taski
ng.
Exampl
eforMul
ti
taski
ng-

I
nonl
i
nesessi
on,
whatar
ethedi
ff
erentact
ivi
ti
esdonebyst
udent
sas?
- Li
stent
hecl
ass
- Taki
ngr
unni
ngnot
es
- Checki
ngmobi
l
e

Pr
ocessbased-
1.Executi
ngseveralt
askssimul
taneousl
ywher
eeachtaski
s
separat
eindependentpr
ocesssuchasmulti
taski
ngi
scal
ledas
processbased.
2.Exampl
e1-Typi
ngj
avapr
ogram i
ntoecli
pse,
alsol
i
steni
ngt
he
audi
osongs,
downl
oadafi
lefr
om int
ernet
.
3.I
nthi
sev
eryact
ivi
tyi
sindependentpr
ocessher
e.
4.Example-2Taskmanager,
seet
hemul
ti
plepr
ocessl
i
st.
(Cont
rol+Shi
ft
+Esckey)
5.Pr
ocessi
sheav
ywei
ghtcomponent
s.
6.Eachpr
ocesshasownaddr
essi
ntomemor
y.

Thr
eadbased-
1.Executi
ngsever
altaskssi
multaneousl
ywhereeachtaski
s
separat
epartofsameprogram call
edast
hreadbased.
2.Example-supposeIhave1000l i
nesofcodei ntojav
apr ogram and
i
twillt
ake8hour stoexecuteitwherefir
st500lineisexecuted
aft
erthatremaini
ng500linesisexecutedbutthereisnoany
dependencybetweenthem soIcanr unthattaskssimultaneousl
y
tominimizetheexecut
iontime.
3.Thr
eadi
sli
ghtwei
ghtcomponent
s.
4.Thr
eadshar
est
hesameaddr
essspace.

Whati
sthr
ead?
I
tist
hesmal
l
estuni
tofpr
ogr
am cal
l
edasThr
ead.

Howt
ocr
eat
etheThr
ead?
Ther
ear
etwoway
stocr
eat
ethet
hreadas

1.Byext
endi
ngThr
eadcl
ass
2.Byi
mpl
ement
ingRunnabl
eint
erf
ace.

1.Byext
endi
ngThr
eadcl
ass

Thr
eadclasspr
ovi
deconst
ructor
sandmethodstocreat
eandper
form
Oper
ationsonathr
ead.Thr
eadcl
assextendsObj
ectcl
assandimplement
sRunnab
i
nter
face.

Const
ruct
ors

o Thr
ead(
)

o Thr
ead(
Str
ingname)

o Thr
ead(
Runnabl
er)
o Thr
ead(
Runnabl
er,
Str
ingname)

Met
hods-

1.publ
icv
oidr
un(
):
 i
susedt
oper
for
m act
ionf
orat
hread.
2.publ
icv
oidst
art(
):
 st
art
stheexecut
ionoft
het
hread.
JVM cal
l
s
ther
un()met
hodonthethread.
3.publi
cv oi
dsleep(
longmi l
li
seconds):
 Causest
hecurr
ent
ly
executi
ngthr
eadtosleep(temporari
l
yceaseexecut
ion)f
orthe
specif
iednumberofmill
i
seconds.
4.publ
icv
oidj
oin(
):
 wai
tsf
orat
hreadt
odi
e.
5.publi
cvoidj
oin(
longmill
iseconds)

wai
tsf
orat
hreadt
odi
efor
thespeci
fi
edmil
li
seconds.
6.publ
ici
ntget
Pri
ori
ty(
):
 r
etur
nst
hepr
ior
it
yoft
het
hread.
7.publ
ici
ntset
Pri
ori
ty(
intpr
ior
it
y):
 changest
hepr
ior
it
yoft
het
hread.
8.publ
icSt
ri
ngget
Name(
):
 r
etur
nst
henameoft
het
hread.
9.publi
cvoi
dset
Name(
Str
ingname)

changest
henameoft
he
thr
ead.

Pr
ogr
am f
orThr
ead-

packagecom.
thr
eads;

publ
iccl
assThr
eadDemoext
endsThr
ead{

publ
icv
oidr
un(
){

f
or(
inti=1;i
<=10; i
++){
Sy
stem.out
.pr
int
ln(
i)
;
}
}

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
ThreadDemothread=newThr
eadDemo(
);
thr
ead.st
art
();
}
}

2.Byi
mpl
ement
ingRunnabl
eint
erf
ace.

TheRunnablei
nter
faceshouldbei
mplement
edbyany
cl
asswhoseinstancesar
eint
endedtobeexecut
edbyat hr
ead.
Runnabl
eint
erf
acehasonlyonemet hodnamedrun(
).

Pr
ogr
am-
1

packagecom.
thr
eads;

publ
iccl
assThr
eadDemoi
mpl
ement
sRunnabl
e{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

ThreadDemot
hread=newThreadDemo(
);
Threadt=newThread(
thr
ead)
;
t.
start
();
}

@Overr
ide
publ
icvoi
drun(
){

f
or(
inti=1;i
<=10; i
++){
Sy
stem.out
.pr
int
ln(
i)
;
}

}
}

Not
e-I
fyouar
enotext
endi
ngt
heThr
eadcl
ass,
yourcl
assobj
ectwoul
d
notbetr
eatedasat hr
eadobject.So,youneedtoexpl
i
cit
lycr
eateThr
ead
cl
assobject
.Wear epassingtheobjectofyourcl
asst
hatimpl
ements
Runnabl
esot haty
ourclassrun()met hodmayexecut
e.

When?

Ext
endi
ngthr
eadclass-i
fthecl
assi
snotext
endi
nganot
hercl
asst
hen
weshoul
dgofort
hreadclass.

Impl
ement i
ngrunnabl
einter
face-i
fourclassisalreadyextendi
ng
anot
herclassthenwecouldnotuseextendkey wordduet omulti
ple
i
nheri
tance.Sobestwaytogof orr
unnableint
erface.

Dif
fer
encebetweent
hreadcl
assandr
unnabl
eint
erf
ace(
Sel
f
assi
gnments)

Exampl
e-Pr
ogr
am f
ormul
ti
thr
eadi
ng
packagecom.
mul
ti
;

publ
iccl
assMul
ti
thr
eadi
ngExampl
eext
endsThr
ead{

publ
icv
oidr
un(
){

f
or(i
nti =1; i<=5;i++){
try{
Thread.Sleep(500);//i
twi
l
lpausethet
hread
ex
ecut
ionf
orpart
icularmi l
li
seconds
}cat ch(Exceptione){
System.out.pr
int
ln(
e.get
Message(
));
}
Sy st
em. out.
printl
n(i
);
}
}

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Mult
it
hreadi
ngExampl
ethr
ead1=new
Mul
ti
thr
eadi
ngExample(
);
Mult
it
hreadi
ngExampl
ethr
ead2=new
Mul
ti
thr
eadi
ngExample(
);
t
hread1.
star
t()
;
t
hread2.
star
t()
;

Out
put
-
1
1
2
2
3
3
4
4
5
5

Adv
ant
agesf
ormul
ti
thr
eadi
ngi
njav
a

Theuser
sar
enotblockedbecauset
hreadsar
eindependent
,andwecan
per
for
m mul
ti
pleoperat
ionsatt
imes

Pr
ogr
am-
3

packagecom.
thr
eads;

publ
iccl
assThr
eadDemoext
endsThr
ead{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

ThreadDemothread=newThreadDemo();
System.out
.pr
int
ln(
thr
ead.
get
Id(
));
System.out
.pr
int
ln(
thr
ead.
get
Name( )
);
System.out
.pr
int
ln(
thr
ead.
get
Pri
ori
ty()
);
Sy
st out
em. .pr
int
ln(
thr
ead.
get
Stat
e()
);

}
}

Out
put
-

12
Thr
ead-
0
5
NEW

You might also like