Time Travel - Predicting the Future and Surviving a Parallel Universe - JDC2012

2,246 views

Published on

Published in: Technology, Travel
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,246
On SlideShare
0
From Embeds
0
Number of Embeds
1,065
Actions
Shares
0
Downloads
13
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Time Travel - Predicting the Future and Surviving a Parallel Universe - JDC2012

  1. 1. Time Travel!Predicting the Future !and Surviving a Parallel Universe ! Hossam Karim!
  2. 2. The Free Lunch is Over!!
  3. 3. Welcome to the Real World!
  4. 4. Concurrency!
  5. 5. It’s Pretty Hard!
  6. 6. Concurrency Pain!
  7. 7. Doubled-Checked Locking Problem!"#$%&!&$((!"#$%&()*+,-#*+./0!1!!!!!!!)%*+,!234/5.675#$8*!34/5.675#$8*9!!!!!!!"#$%&!234/5.675#$8*!0:234/5.675#$*;<!1!!!!!!%-!;34/5.675#$8*!==!."$$<!!!!!!!!34/5.675#$8*!=!.,/!234/5.675#$8*;<9!!!!!!),+").!34/5.675#$8*9!!!!>!!!!>!
  8. 8. Doubled-Checked Locking Problem!"#$%&!&$((!"#$%&()*+,-#*+./0!1!!!!!!!)%*+,!234/5.675#$8*!34/5.675#$8*9!!!!!!!"#$%&!234/5.675#$8*!0:234/5.675#$*;<!1!!!!!!%-!;34/5.675#$8*!==!."$$<!1!!!!!!!!(/.&0)1.%2,3!;"#$%&()*+,-#*+./0>&$((<!1!!!!!!!!!34/5.675#$8*!=!.,4!234/5.675#$8*;<9!!!!!!!!?!!!!!!?!!!!!!),+").!34/5.675#$8*9!!!!?!!!!?!
  9. 9. Doubled-Checked Locking Problem!"#$%&!&$((!"#$%&()*+,-#*+./0!1!!!!!!!)%*+,!234/5.675#$8*!34/5.675#$8*9!!!!!!!"#$%&!234/5.675#$8*!0:234/5.675#$*;<!1!!!!!!%-!;34/5.675#$8*!==!."$$<!1!!!!!!!!(/.&0)1.%2,3!;"#$%&()*+,-#*+./0>&$((<!1!!!!!!!!!!%-!;34/5.675#$8*!==!."$$<!!!!!!!!!!!!34/5.675#$8*!=!!!!!!!!!!!!!!!!!.,4!234/5.675#$8*;<9!!!!!!!!?!!!!!!?!!!!!!),+").!34/5.675#$8*9!!!!?!!!!?!!!!
  10. 10. Doubled-Checked Locking Problem volatileThread AThread B Processor Main Cache Memory synchronized
  11. 11. Doubled-Checked Locking Problem!"#$%&!&$((!"#$%&()*+,-#*+./0!1!!!!)%*+,!*-$+%$,!234/5.675#$8*!34/5.675#$8*9!!!!!!!"#$%&!234/5.675#$8*!0:234/5.675#$*;<!1!!!!!!%.!;34/5.675#$8*!==!/"$$<!1!!!!!!!!(0/&1)-/%2,3!;"#$%&()*+,-#*+./0>&$((<!1!!!!!!!!!!%.!;34/5.675#$8*!==!/"$$<!!!!!!!!!!!!34/5.675#$8*!=!!!!!!!!!!!!!!!/,4!234/5.675#$8*;<9!!!!!!!!?!!!!!!?!!!!!!),+")/!34/5.675#$8*9!!!!?!!!!?!
  12. 12. Scala!"#$$!"#$%$&(()*+,%(-.(#/(01*#/234!5!!!!!!!!"#%&!#"!(67(3829(:(8*+#(!;!()*!<67(3829(:(8*+#(!!!=!! Decompiled into Java!!"#$$!"#$%$&(()*+,%(-.(#/(01*#/234!%&"(&()*$!!"#$#%&(")!5!!!!+%,#*(!678(392:(;(9*+#(!(78(392:(;(9*+#(<!!!!-."%!!,/"#*%"(!%)*!,2=>$8?@<!!!!!!-."%!!678(392:(;(9*+#(!(78(392:(;(9*+#(AB!5!!!!!!%0!AA*1%$C,2=>$8?@!D!@7EB!FF!@B!!!!!!$2)!1+/)%3(4!A*1%$B!5!!!!!!!!%0!AA*1%$C,2=>$8?@!D!@7EB!FF!@B!5!!!!!!!!!!*1%$C(78(392:(;(9*+#(!F!)(5!678(392:(;(9*+#(AB<!!!!!!!!!!*1%$C,2=>$8?@!GF!E<!!!!!!!!H!!!!!!!!+(*-+)!*1%$C(78(392:(;(9*+#(<!!!!!!H!!!!H!!H!!!!
  13. 13. Concurrency Models!
  14. 14. Concurrency Models !"#$%&&#()*&"+&,--.#+) /"01 !"--%#.$,(."# 23#$4&"#.5,(."#24,&0)/-"&3 /66,+)*,66.#+ 7%6389,.(.#+ 71"$:.#+ 2-,;4"&6 /"#.("&6
  15. 15. Multi-threading Approach!
  16. 16. Thread Pools and Executors!!!!"#!"#$%&()%*&+,,-./#0%12!34/5!4()%*&12!34/6!7!!!!!!$%&!8,,-!9!:$%;</,)1=4%>?#$%&()%*&+,,-.4()%*&16!!!!!!$%&!)%1<-/!9!@%4;(0*)A./#0%)*B%-C%"#4#/#,46!7!!!!!!!!#48</!!!!!!!!!!!!#(!.D!E!F!/,!/#0%16!7!!!!!!!!!!!!8,,-=%$%;</%!7!!!!!!!!!!!!!!!!)"*!!"##$%&!7!!!!!!!!!!!!!!!!!"#!)<4.6!7!!!!!!!!!!!!!!!!!!%4;&%;.#48</6!!!!!!!!!!!!!!!!G!!!!!!!!!!!!!!G!!!!!!!!!!!!!!G!!!!!!!!!!G!!!!!!!!!!8,,-=1(</&,>4.6!!!!!!!!!!8,,-=*>*#/%)0#4*/#,4.F5!()*+#),=H3IJ:K6!!!!!!G!!!!!!-,L=#4",.M"#$%&()%*&+,,-2!C,4%!N&!O,@1!<1#4L!N&!()%*&1!#4!N&!01M=!!!!!!!!",)0*/./#0%15!4()%*&15!)%1<-/=/#0%H#--#166!!!G!!!
  17. 17. Java Fork-Join Framework!
  18. 18. Fork-Join Framework join fork joinForkJoinTask fork fork join Deque Worker per Worker Thread Thread Pool !"#$% !&!%()*+,% fork -./0 *)*+,% Work Stealing !&!%()*+,% !"#$% fork
  19. 19. Fork-Join Framework!!!"#$$!"#$%&()#*%+,-./!!!#0#+#123!4&&-5671289!2-&23!7129!#1:3!7129!2;&#;<0:3!712=!!!!!%&%()$!!"#$%&()#,-.6><1?8!@!!!!!!!!-#&2/!!!!!!!2;&#;<0:!AB!CDD!EE!2;&#;<0:!FB!CDDD9!!!!!!!!G,;&#;<0:!+%2!H#0<1?!2<!6CDD9!CDDD8G=!!!!!!!!)%*!$<+I%2#3!><1?!B!@!!!!!!!!+*!//#1:!J!2-&2=!F!2;&#;<0:=!!!!!!!!!!#0#+#12K0($#/2-&29!#1:=K%+!!!!!!!!%"$%!@!!!!!!!!!!,#"!+(::0#!B!/#1:!L!2-&2=!AAA!C!!!!!!!!!!,#"!0#M2!B!!(%-!"#$%&()#*%+,-./#0#+#129!2-&29!+(::0#9!2;&#;<0:=!!!!!!!!!!,#"!&(?;2!B!(%-!"#$%&()#*%+,-./#0#+#129!+(::0#9!#1:9!2;&#;<0:=!!!!!!!!!!!!0#M2KM<&.!!!!!!!!!!&(?;2KM<&.!!!!!!!!!!!!0#M2KN<(1!L!&(?;2KN<(1!!!!!!!!O!!!!!!O!!O!
  20. 20. Fork-Join Framework!!!"#!"#$%&#()*+,-.-+-(/01!2(/3!/4$-04#.51!2(/3!67$7..-.0+1!2(/8!9!!!!!!!$%&!6##.!:!"(!;#$%&#(<##.,67$7..-.0+8!!!!!!!$%&!0*+=70%!:!!!!!!!!!"(!>-?*$0@-)*+=70%,,A!/#!-.-+-(/08B/#C$$7D3!E3!-.-+-(/03!/4$-04#.58!!!!!!!$%&!$-0*./!:!F-(?4+7$%,0*+=70%8!9!!!!!!!!(6*/!!!6##.B(@#%-,(6*/8!!!!!!G!!!!!!!6##.B04*/5#H(,8!!!!!!!.#IB("#,!!!!!!!!!J"#$%&#()*+1!0*+!:!K53!5#(-!(!K5!+0JB!!!!!!!!!!!!"#$+7/,$-0*./B@7.*-3!$-0*./B/+-L..088!!!!!G!!!!
  21. 21. Parallel Collections!
  22. 22. Parallel Collections - Mathematica ParametricPlot3D CosΦ SinΘ, SinΦ SinΘ, CosΘ, In[1]:= plotSpherefactor_ : Φ, 0, 2 Π, Θ, 0, Π, PlotPoints 100, Mesh None, ColorFunction Functionx, y, z, Φ, Θ, HueSinfactor Φ Sinfactor Θ, ColorFunctionScaling False, Axes None, Boxed False, ImageSize 100 LaunchKernels DistributeDefinitionsplotSphereOut[2]= KernelObject1, local, KernelObject2, local, KernelObject3, local, KernelObject4, local, KernelObject5, local, KernelObject6, local, KernelObject7, local, KernelObject8, localOut[3]= plotSphere
  23. 23. Parallel Collections - Mathematica
  24. 24. Parallel Collections - Mathematica
  25. 25. Scala Parallel Collections!!!#!#$%#$()#*+!#,!-#%*+!$%().+!*+%,/#0!1!2!!!!!!-./!(34%56),!78%94%56).!1!!!!!!!!:5%8*()#*;#$%+!=.;!!!!!!!!!!34(!!!!!!!!!!!!-#!!!(0!#(?@$A@$?;-8*34%()#*;B%C4%6,!-#...;!!!!!!!!!!4*%#%#87(;B%D43;7)$E#%6(-#%*..!!!!!!!!-./!56#)F#*5%8*#$!1!78%94%56);4BB*B4%(:5%8*;3%G/#0.(!!!!!!2!!!!!!!!(H5%8*,!-#.!!!!!!!!!!!!#!(-#;#$F#*5%8*G.!H5%8*!+I!-#!/1!H5%8*!!!!!!J!!!!!!,!!II!.!!!!!!!!34%56)!II!56#)F#*5%8*#$;-8)K-%(:5%8*;3%G/#0.!2!!!!!!!!(H5%8*,!)#*.!!!H5%8*!II!#$%#$()#*,!-#%*.!!!!!!J!!!!J!
  26. 26. Scala Parallel Collections!!!#$%()!!!!!#!%*#+%(*,()-!!!!!!$)).!$%(/+%(01!2%*.!$%(/+%(01!3%#(.!)(*+,4.!$%(/+%(0!5!6!!!!!!!!!#!*7%#-2%.!+%(4.!-$%(/+%(01!$%(/+%(04!5!6!!!!!!!!-./!-8$#)9(21!:;#$#)9(24!5!!!!!!!!!!=()#;-2%%*#.!?@4!!!!!!!!!!!!8$7-!!!!!!!!!!!!!!3%(!!!+0!+%(-AB*CB*A3;8$#-2%D(#E$#91!3%(444!!!!!!!!!!!!7$#%#%;:-?D(#F$8((:2*G%#9-3%#(44!!!!!!!!!!-8$#)9(21!:;#$#)9(24!!!!!!H!!!!!!!!*#!-2%*%*I87#J4!!!!!!!!$))!!!!!!/1!6!!!!!!!!-./!-8$#)9(21!:;#$#)9(24!5!!!!!!!!!!2%*8$7-*7%#4(2K)(L(3#--M1!J4!!!-M?N!OO!J?N1!M?P!OO!J?P44!!!!!!!!!!-./!)9%2Q%()#;%(*!5!:;#$#)9(2$DD(D$#(-=()#;(87#J/+%(04-!!!!!!!!6!!!!!!!!!!-R()#;1!3%(4!!!!!!!!!!!!!!*#!-3%(%*Q%()#;J4!R()#;!.O!3%(!/1!R()#;!!!!!!!!H!!!!!!!!1!?!OO!?4!!!!!!!!!!%*#+%(*,()-$))!OO!8$#)9(21!)9%2Q%()#;%(*1!3%#(4!!!!!!H!!!!H!
  27. 27. Scala Parallel Collections!!!!#!#$%#$()*)+,#*-!#.!/#%*-!$%()0-!()*12%3*4#5!6!7!!!!!!*+,!+8)%29,.!:3%;)%29,0!6!!!!!!!!12%3*+,#*#$%-!=0-+!!!!!!!!!!8)?+!!!!!!!!!!!!/#!!!(.!#+@A$BA$@/3*8)%+,#*C%()%9.!/#000!!!!!!!!!!?)*%#%#3:+=C%D)8:,$E#%9+/#%*00!!!!!!!!*+,!29#,F#*2%3*#$!6!:3%;)%29,-+)CC*C)%+12%3*8?%G4#50+!!!!!!7!!!!!!!!+H2%3*.!/#0!!!!!!!!!!!!#!+/##$F#*2%3*G0!H2%3*!-I!/#!,/!H2%3*!!!!!!J!!!!!!.!=!II!=0!!!!!!8)%29,!II!29#,F#*2%3*#$-+/)%;)?+#$%#$()*)+=.!/#%*00!!!!J!!!!
  28. 28. Scala Parallel Collections
  29. 29. Message Passing!
  30. 30. Akka Actors !#$%(#)* !#$% !#$% !#$%2%$3/)456$%7 ()%*+,$%, 8%)970$:1 ,9/9#+$1 ;),($1,) -.+/0%)1 !#$% !#$% !#$% !#$% !#$% !#$% ! 3).9*+$%@ !#$% %))+*) !#$% =9+/3$? =),,9) %)(/A@4),,9)
  31. 31. Akka Actors!!#$%!#$$!#$%())#*+$))#*,!()*+,-!!!!!!-%.!).$%/01234)1$+-!5!!!!!!/#!)4)1$!6!/01234)1$+7$#.8/01234)1$7-!!!!!!!/#!)#$%/0123!6!!!!!!!!!)4)1$9#0123:;+32%)=#$%/0123?!8#$!6!7)#$%/01237-!!!!!!!/#!@*#1/0123!6!!!!!!!!!)4)1$9#0123:;+32%)=A*#1/0123?!8#$!6!7@*#1/01237-!!!!!!!)#$%/0123!B!7C27!!!!!!)#$%/0123!B!#$%())#*+7DAEFGHIG7-!!!!!!!)4)1$9)CJ1@2K8+-!!L!!!!!
  32. 32. Akka Actors!!!#$$!#$%()*+,!%%()$!!#$%!-!!!!!!*#!+.!/!0+..12.3)+2*4*5676*$8!+,$9!!!!!!*#!:.#*()*+,!/!!!!!!!!)+2*4*5#)*+,;+,3#==#??$#12()*+,76*$?@6,?:.#*()*+,9!!!!!)%-!,)1A!/!-!!!!!!!#$%!6!./,(0!!!!!!!!!!+.512B+369!!!!!!!!#$%!+*C,!!!!!!!!!!!!!!+.512B+3D#2E*!C#2:!*C16!+28!F1!:.#*!*+!#2+*C,!#)*+,9!!!!!!!!:.#*()*+,!G!+*C,!!!!H!!H!!!!
  33. 33. Akka Actors!!!#$$!#$#%#()*+!%%()$!!#$%!,!!!!*#!$*%!-!.*%%/0%1)*0#23454#67!+,$8!!!!!!)%-!+#)#/9#!-!,!!!!!!!!!#$%!:6;$##44%#16#44%#8!!!!!!!!!!$*%3/0=*1?#)#/9#@!6#44%#!A43=*+616#44%#88!!!!!!B!!B!!!!
  34. 34. Akka Futures ./0$1-$2!#$%3)*4,$5 !#$%+,*$- !#$%3)*4,$./0$1--)6(%!#$%#(()(* 76(%4#-(189$:4$%- 9$16;;$(2-)6(% 3)((1)# 76(%4#-(-%
  35. 35. Akka Futures ! !!#!#$#%$%($)*+,!!#$%-(#-$%($)./0$*1233!4! !!!$%!5! %#$%+,*$-./-0, !!!!!(/+6*)#%78(/#9!:!! !!!!!!!7+(/+;(%)$0$1%!=!(/+6*)#%?#+$7()!@($99AB(C;1-(/+6*)#%3! !!!!!!!!!+$%($)*+!:!! !!!!!!!!!!!6#$#%A$%(8%+7012+#/$(20.0$1%+97! !!!!!!!!!!!!!(0$1%!!!7(0$1%!=!$%($).?#+$78(/#99AB(C;1-$%($)./0$*1239! !#$%1)*2,$3$42$%- !!!D!*+(#,!+$%($)*+! !! !#$%1)*2,$ -!#!#$#%01BB2E($*12+,!!#$%-(#-)#$*#%+,33!4! !!!#$#%$%($)*+AB(C7! !!!!!/*+$F$%($)./0$*12!! /*+$F$%($)./0$*12A/($G(C7HA%01BB2E($*12+99! !! !#$%#(()(* !-,-$*53$42$%- !3$/066$(7-)0(% /0(%2#-(/5./-0, /0(%2#-(/5./-0, /0(%2#-(/5./-0,
  36. 36. Akka Futures!!!#!#$#%$%($)*+,!!#$%-(#-$%($)./0$*1233!4!!!!$%!5!!!!!!!(/+6*)#%78(/#9!:!!!!!!!!!7+(/+;(%)$0$1%!=!(/+6*)#%?#+$7()!@($99AB(C;1-(/+6*)#%3!!!!!!!+$%($)*+!:!!!!!!!!!6#$#%A$%(8%+7012+#/$(20.0$1%+97!!!!!!!!!!!!!!(0$1%!!!7(0$1%!=!$%($).?#+$78(/#99AB(C;1-$%($)./0$*1239!!!!!D!*+(#,!+$%($)*+!!!-!#!#$#%01BB2E($*12+,!!#$%-(#-)#$*#%+,33!4!!!!#$#%$%($)*+AB(C7!!!!!!/*+$F$%($)./0$*12!!/*+$F$%($)./0$*12A/($G(C7HA%01BB2E($*12+99!!!
  37. 37. ! Akka Promises!!!!#$!!%##!#$#%($)%*+!()*+,!!!!!!!%##!-.(*/01*0#!2!!!!!!!,%!3#*1!4!56#./3#*17$88$7,!!!!!!)-.%)!)!,%!*6!4!3#*19:;$#6=*/!!!!!!!!!/$0!)*$**))?@%/A%#%/*+!!#$%B#$#%C!4!2!!!!!!!!,%!D.(*/01*0#E/.1;*!4!E/.1;*B#$#%C,!!!!!!!!A%#%/*!2!:..1*FG#/*1*)3H$/:#%IID.(*/01*0#E/.1;*,!J!!!!!!!!).D9;0I.7K*L/*!#=*!D.(*/01*0#M!:.0L#!6$))!%M!N*!N;))!6$))!3.%7,!!!!!!!!D.(*/01*0#E/.1;*!!!!!!J!!!!!!!!/$0!:..1*FG#/*1*)3H$/:#%II/.1;*+!!#$%B#$#%C,!2!!!!!!!!).D9;0I.!!!!!!!!!!7K*L/*!#=*!D.(*/1*0#M!N*!);(*!;0!$!$/$))*)!N./):!6$))*:+!O79!!!!!!!!!!!!I./1$#=/*$:96%//*0#=/*$:,9D*#P$1*,,!!!!!!!!).D9;0I.!!!!!!!!!!7K*L/*!#=*!D.(*/01*0#M!N*L/*!D.;0D!#.!)**!I./!$!N=;)*7,!!!!!!!!=/*$:9)**QRRR,!!!!!!!!/.1;*9%66*#$#%7K.0:*/I%)7,,!!!!!!J!!!!!!!!/$0!#=$08503N$3,!2!!!!!!!!3#*19=%#:.N0,!!!!!!J!!!!J!!!!
  38. 38. Akka Promises!!!!!#!#$%()*+,-!.!!!!!!!!$%!/)0$#*1$*(!2!(!3)0$#*1$*(!!!!!!!!$%!/)0$#*1$*(4#)1+$!2!/)0$#*1$*(56$7+$8$669+:;#;(;#$!!!!!!!!6)/5*=),?$@#$!(A$!$)6$B!C$!7+D$%!E);!7!F;$+()*-!!!!!!!!!!#$%()*+$*,-./%0+1-+12($2+$$-34)%*)%/05$$6)--3-3!!!!!!GC7(5#$7%E,/)0$#*1$*(4#)1+$B!HI!+$)*%+-!!!%/786-5/1!!!!!!!!/)0$#*1$*(4#)1+$!)*J;$++!.!!!!!!!!)%*!J(7(;+,076;$-!!!!!!!!!!!!6)/5*=),!!!!!!!!!!!:;#!/)0$#*1$*(!=*766E!#$6$%B!(A$E!+7E!(@+!K+5=)#17(,076;$--!!!!!!L!)*76;#$!.!!!!!!!!)%*!$!!!!!!!!!!!!6)/5$##)#,:)+B!+)##E5!3))%!6;D!*$M(!(1$!N--!!!!!!L!!!!!!!!/)0$#*1$*(5(A7*D+G*EC7E,-!!!!L!!!!!!
  39. 39. Relations to Functional Programming!Selected  Topics  
  40. 40. Monoid !#$%() !*%%$+,-.,/,.0) !12(3.,.0)!#$#%((!)#*#+ !#$#%#)#*#+,!#$((!%#)-((!*#+ !#$#%#)#*#+!#$((,!%#)((!*#+-!#$#%((! !#$#%!((!#$#%
  41. 41. Monoid !#$%()*%+,-.+.//%/0 1%+2%
  42. 42. Immutable Data Structures – Google’s Guava!!!#$%!!#$#$%()!**+$,-./01../2$1345!6!!!!!!!#$#$%()!3$%(3!7!!!!!!!!%$()*+!#$8#$%())*,+-458!!!!!!!!!!,994:;11(./3:58!!!!!!!!!!,994:;+,=,:58!!!!!!!!!!,994:+.9/%3:58!!!!!!!!!!-+.945?!!!!!!..#$-/0#1(,,2345$675##)*+389!!!!!!()*!3$%(3?!!!!@!!! h.p://code.google.com/p/memory-­‐measurer/wiki/ElementCostInDataStructures  ! Immutable Data Structures – Scala!!!#!#$%$()$*%+,-)!.!!!!/0#$%$/!11!/()$*%+/!11!/,-)/!11!2-%!!!! h.p://en.wikipedia.org/wiki/Persistent_data_structure  
  43. 43. Persistent Data Structures ! % !!#!#$%$()$*+!,!-.-!//!-0-!//!-1-!//!-2-!//!3$(! !!!!!! !!#!4)$*+!,!-5-!//!#$%$()$*+! !!! !!#!6)$*+!,!#$%$()$*+!$%!7! !!!!()!89:!//!+$(!!!-;-!//!+$(! !!!!()!!!!!!!!!!!!!!!-;-!//!3$(! !=! !!!!!!# !!#!)$*+!,!#$%$()$*+!$%!7! !!!!()!?$#*+!//!*9@:!//!+$(!!!-A-!//!+$(! !!!!()!!!!!!!!!!!!!!!!!!!!!!!!!!-A-!//!3$(!$ !=! !! !
  44. 44. Thank You!

×