Você está na página 1de 12

A walkLhrough for Lhe fork/[oln framework lnLroduced ln !

ava SL 7
osLed on AugusL 2 2011
by Masoud kalall
ln Pow 1o !ava !ava SL Learnlng
0
lnShare
ulgg ulgg

!ava SL 7 broughL some neaL feaLures on Lhe Lable for !ava developers one of Lhese feaLures ls Lhe
fork/[oln framework or baslcally Lhe new parallel programmlng framework we can use Lo easlly Lo
lmplemenL our dlvlde and conquer soluLlons 1he polnL ls LhaL a soluLlon Lo a problem should be
devlsed wlLh Lhe followlng characLerlsLlcs Lo use Lhe fork/[oln framework effecLlvely

* 1he problem domaln wheLher lL ls a flle llsL eLc Lo be processed or a compuLaLlon should be
dlvldable Lo smaller subLasks
* rocesslng chunks should be posslble wlLhouL requlrlng Lhe resulL of oLher chunks

1o summarlze lL solvlng or processlng Lhe problem domaln should requlre no selffeedback Lo make
lL posslble Lo use Lhe framework lor example lf you wanL Lo process a llsL and processlng each
elemenL ln Lhe llsL requlre Lhe resulL of processlng prevlous elemenL Lhen lL ls lmposslble Lo use any
parallel compuLlng for dolng LhaL [ob lf you wanL Lo apply some ll1 over a sound sLream whlch
requlre feedback for processlng each pulse from Lhe prevlous pulses lL ls noL posslble Lo speedup Lhe
processlng uslng Lhe fork/[oln framework eLc

Well before we sLarL learnlng Lhe fork/[oln framework we beLLer know whaL lL ls and whaL lL ls noL
WhaL fork/[oln framework ls

* A parallel programmlng framework for !ava
* arL of !ava SL 7
* SulLable for lmplemenLlng parallel processlng soluLlons mosLly daLa lnLenslve wlLh small or no
shared resources beLween Lhe workers who process Lhe daLa chunks
* SulLable when no synchronlzaLlon ls requlred beLween Lhe workers

WhaL fork/[oln framework ls noL

* lL ls noL a maglc LhaL Lurns your code Lo run fasL on machlnes wlLh mulLlple processors you need
Lo Lhlnk and lmplemenL your soluLlons ln a parallel manner
* lL ls noL hard and obscure llke oLher frameworks Ml for example uslng Lhe framework ls way
easler Lhan anyLhlng l used before

lf you wanL Lo learn Lhe mechanlcs behlnd Lhe fork/[oln framework you can read Lhe orlglnal arLlcle
wrlLLen by uoug Le whlch explalns Lhe moLlve and Lhe deslgn 1he arLlcle ls avallable aL
hLLp//geecsoswegoedu/dl/papers/f[pdf lf you wanL Lo see how we can use Lhe framework Lhen
conLlnue on readlng Lhls arLlcle

llrsL leL's see whaL are Lhe lmporLanL classes LhaL one need Lo know ln order Lo lmplemenL a dlvlde
and conquer soluLlon uslng fork/[oln framework and Lhen we wlll sLarL uslng Lhose classes

* 1he lork!olnool 1hls ls Lhe workers pool where you can posL your lork!oln1ask Lo be execuLed
1he defaulL parallellsm level ls Lhe number of processors avallable Lo Lhe runLlme
* 1he 8ecurslve1askv 1hls ls a Lask subclass of Lhe lork!oln1ask whlch can reLurn some value
of Lype v lor example processlng a llsL of u1Cs and reLurnlng Lhe resulL of process
* 1he 8ecurslveAcLlon AnoLher subclass of Lhe lork!oln1ask wlLhouL any reLurn value for
example processlng an array

l looked aL Lhls new Al malnly for daLa plpellnlng ln whlch l need Lo process a preLLy huge llsL of
ob[ecL and Lurn lL Lo anoLher formaL Lo keep Lhe processlng resulL of one llbrary consumable for Lhe
nexL one ln Lhe daLa flow and l am happy wlLh Lhe resulL preLLy easy and sLralghL forward

lollowlng ls an small sample showlng how Lo process a llsL of 8ow ob[ecLs and converL Lhem a llsL of
LnLlLy Cb[ecLs ln my case lL was someLhlng slmllar wlLh processlng 8ow ob[ecLs and Lurnlng Lhem Lo
CuaLa CLnLlLy ob[ecLs


1
2
3
4
3
6
7
8
9
10
11
12
13
14
13
16
17
18
19
20
21
22
23
24
23
26
27
28
29
30
31
32
33
34
33
36
37
38
39
40
41
42
43
44
43
46
47
48
49
30
31
32
33
34
33
36
37
38
39
60
61
62
63
64
63
66
67
68
69
70
71
72
73
74
73
76
77
78
79
80
81
82
83
84
83
86
87
88
89
90
91
92
93
94
93
96
97
98
99
100
101
102
103
104
103
106
107
108
109
110
111
112
113
114
113
116
117
118
119
120

lmporL [avauLllArrayLlsL
lmporL [avauLllLlsL
lmporL [avauLllconcurrenLlork!olnool
lmporL [avauLllconcurrenL8ecurslve1ask

/**
*
* [auLhor Masoud kalall mkalall
*/
class 8owConverLer exLends 8ecurslve1askLlsLLnLlLy

//lf more Lhan 3000 we wlll use Lhe parallel processlng
sLaLlc flnal lnL SlnCLL_18LAu_1C 3000
lnL begln
lnL end
LlsL8ow rows

publlc 8owConverLer(lnL begln lnL end LlsL8ow rows)
Lhlsbegln begln
Lhlsend end
Lhlsrows rows


[Cverrlde
proLecLed LlsLLnLlLy compuLe()

lf (end begln SlnCLL_18LAu_1C)
//acLual processlng happens here
LlsLLnLlLy preparedLnLlLles new ArrayLlsLLnLlLy(end begln)
SysLemouLprlnLln( beglng + begln + end + end)
for (lnL l begln l end ++l)
preparedLnLlLlesadd(converL8ow(rowsgeL(l)))

reLurn preparedLnLlLles
else
//here we do Lhe dlvldlng Lhe work and comblnlng Lhe resulLs
// speclfles Lhe number of chunks you wanL Lo break Lhe daLa Lo
lnL dlvlder 3000
// one can calculaLe Lhe dlvlder based on Lhe llsL slze and Lhe number of processor avallable
// uslng Lhe
hLLp//downloadoraclecom/[avase/7/docs/apl/[ava/lang/8unLlmehLml#avallablerocessors()
// decrease Lhe dlvlder number and examlne Lhe changes

8owConverLer curLefL new 8owConverLer(begln dlvlder rows)
8owConverLer cur8lghL new 8owConverLer(dlvlder end rows)
curLefLfork()
LlsLLnLlLy lefL8eslL cur8lghLcompuLe()
LlsLLnLlLy rlghL8es curLefL[oln()
lefL8eslLaddAll(rlghL8es)
reLurn lefL8eslL



//dummy converLed meLhod converLlng one u1C Lo anoLher
prlvaLe LnLlLy converL8ow(8ow row)

reLurn new LnLlLy(rowgeLld())



// Lhe drlver class whlch own Lhe pool
publlc class l[f

publlc sLaLlc vold maln(SLrlng args)

LlsL8ow rawuaLa lnlLuummyLlsL(10000)
lork!olnool pool new lork!olnool()
SysLemouLprlnLln(number of worker Lhreads + poolgeLarallellsm())

LlsLLnLlLy res poollnvoke(new 8owConverLer(0 rawuaLaslze() rawuaLa))

// add a breakpolnL here and examlne Lhe pool ob[ecL
//check how Lhe sLealCounL whlch shows number of subLasks Laken on by avallable workers
//changes when you use an smaller dlvlder and Lhus produce more Lasks
SysLemouLprlnLln(processed llsL + resslze())



/**
* creaLes a dummy llsL of rows
*
* [param slze number of rows lnL he llsL
* [reLurn Lhe llsL of [see 8ow ob[ecLs
*/
prlvaLe sLaLlc LlsL8ow lnlLuummyLlsL(lnL slze)

LlsL8ow rows new ArrayLlsL8ow(slze)

for (lnL l 0 l slze l++)
rowsadd(new 8ow(l))

reLurn rows



//dummy classes whlch should be converLed from one form Lo anoLher
class 8ow

lnL ld

publlc 8ow(lnL ld)
Lhlsld ld


publlc lnL geLld()
reLurn ld



class LnLlLy

lnL ld

publlc LnLlLy(lnL ld)
Lhlsld ld


publlc lnL geLld()
reLurn ld



!usL copy and pasLe Lhe code lnLo your luL and Lry runnlng and examlnlng lL Lo geL deeper
undersLandlng of how Lhe framework can be used posL any commenL and posslble quesLlons LhaL
you may have here and l wlll Lry Lo help you own wlLh Lhem

Você também pode gostar