Your SlideShare is downloading. ×
0
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Pick up the low-hanging concurrency fruit
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Pick up the low-hanging concurrency fruit

1,270

Published on

My presentation on concurrency at GeeCON 2011

My presentation on concurrency at GeeCON 2011

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,270
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. <ul>Concurrency pick the low-hanging fruit </ul><ul>Václav Pech </ul>
  • 2. <ul>About me </ul>Passionate programmer Concurrency enthusiast GPars @ Codehaus lead Groovy contributor Technology evangelist @ JetBrains JetBrains Academy member http://www.jroller.com/vaclav <ul><ul><li>http://twitter.com/vaclav_pech </li></ul></ul>
  • 3. <ul>We’re all in the parallel computing business! </ul>
  • 4. &nbsp;
  • 5. # of cores Today Soon
  • 6. <ul>Dealing with threads sucks! </ul>public class Counter { private static long count = 0 ; public Counter() { count++; } }
  • 7. <ul>Dealing with threads sucks! </ul>public class Counter { private static long count = 0 ; public Counter() { synchronized ( this ) { count++; } } }
  • 8. <ul>Dealing with threads sucks! </ul>public class Counter { private static long count = 0 ; public Counter() { synchronized ( this .getClass()) { count++; } } }
  • 9. <ul>Dealing with threads sucks! </ul>public class ClickCounter implements ActionListener { public ClickCounter(JButton button) { button.addActionListener( this ); } public void actionPerformed( final ActionEvent e) { ... } }
  • 10. <ul>Dealing with threads sucks! </ul>Dead-locks, Live-locks, Race conditions, Starvation, ...
  • 11. <ul>Can we do better? </ul><ul>Map/Reduce </ul><ul>Fork/Join </ul><ul>Actors </ul><ul>STM </ul><ul>Dataflow </ul><ul>Agent </ul>
  • 12. <ul>Asynchronous invocation </ul><ul>Future f = threadPool.submit (calculation); … System.out.println( “Result: “ + f.get()); </ul><ul>main </ul><ul>calculation </ul><ul>get() </ul>
  • 13. <ul>Thread Pool </ul>Tasks Worker threads Queue
  • 14. <ul>Thread Pool </ul>Contention!
  • 15. <ul>Fork/Join Thread Pool </ul>
  • 16. <ul>Fork/Join Thread Pool </ul>Work stealing
  • 17. <ul>Fork/Join </ul><ul><li>Solve hierarchical problems </li></ul><ul><ul><ul><li>Divide and conquer </li></ul><li>Merge sort, Quick sort
  • 18. Tree traversal
  • 19. File scan / search
  • 20. … </li></ul></ul><ul>[a, b, c, d, e, f, g, h] </ul><ul>[a, b, c, d] </ul><ul>[e, f, g, h] </ul><ul>[a, b] </ul><ul>[c, d] </ul><ul>[e, f] </ul><ul>[g, h] </ul>
  • 21. <ul>Collections (Groovy/GPars) </ul><ul>images. eachParallel {it.process()} documents. sumParallel () candidates. maxParallel {it.salary}.marry() </ul>
  • 22. <ul>Parallel Arrays (jsr-166y) </ul><ul>ParallelArray namesOfWomen = people .withFilter( aWoman ).withMapping( retrieveName ).all(); Ops.Predicate aWoman = new Ops.Predicate() { public boolean op(Person friend) {return !friend.isMale();} }; Ops.Op retrieveName = new Ops.Op() { public Object op(Person friend) {return friend.getName();} }; </ul>
  • 23. <ul>Dataflow Concurrency </ul><ul><li>No race-conditions
  • 24. No live-locks
  • 25. Deterministic deadlocks </li></ul><ul><ul><li>Completely deterministic programs </li></ul></ul><ul>BEAUTIFUL code </ul><ul><ul><li>(Jonas Bonér) </li></ul></ul>
  • 26. <ul>Dataflow Variables / Promises </ul><ul>main </ul><ul>task2 </ul><ul>task3 </ul><ul>x </ul><ul>y </ul><ul>z </ul><ul>task1 </ul>
  • 27. <ul>DataFlows (GPars) </ul><ul>def df = new DataFlows () task { df .z = df .x + df .y } task { df .x = 10 } task { <li>println ”I am task 3”
  • 28. df .y = 5
  • 29. }
  • 30. assert 15 == df .z </li></ul>
  • 31. Composing async functions int hash1 = hash(download( &apos;http://www.gpars.org&apos; )) int hash2 = hash(loadFile( &apos;/gpars/website/index.html&apos; )) boolean result = compare(hash1, hash2) println result
  • 32. Composing async functions def hash = oldHash.asyncFun() def compare = oldCompare.asyncFun() def download = oldDownload.asyncFun() def loadFile = oldLoadFile.asyncFun() def hash1 = hash(download( &apos;http://www.gpars.org&apos; )) def hash2 = hash(loadFile( &apos;/gpars/website/index.html&apos; )) def result = compare(hash1, hash2) println result .get()
  • 33. Composing async functions int hash(String text ) {…} Promise&lt;int&gt; hash( Promise&lt;String&gt; | String text )
  • 34. Composing async functions int hash(String text ) {…} Promise&lt;int&gt; hash( Promise&lt;String&gt; | String text ) { <ul><li>Return a Promise for the result
  • 35. Wait (non-blocking) for the text param
  • 36. Call the original hash()
  • 37. Bind the result </li></ul>}
  • 38. Composing async functions Combine functions as usual Parallelism is detected automatically
  • 39. Milestone <ul><li>Asynchronous calculations
  • 40. Fork/Join
  • 41. Parallel collection processing
  • 42. Dataflow variables/streams </li></ul>
  • 43. <ul>Actors </ul><ul><li>Isolated
  • 44. Communicating </li></ul><ul><ul><li>Immutable messages </li></ul></ul><ul><li>Active </li></ul><ul><ul><li>Pooled shared threads </li></ul></ul><ul><li>Activities </li></ul><ul><ul><li>Create a new actor
  • 45. Send a message
  • 46. Receive a message </li></ul></ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>T </ul><ul>T </ul><ul>T </ul><ul>Thread pool </ul>
  • 47. <ul>Stateless Actors (pure Java) </ul><ul>class MyActor extends DynamicDispatchActor { <li>private Account account = ...
  • 48. public void onMessage(String msg ) {
  • 49. String encripted = encrypt( msg );
  • 50. reply( encripted );
  • 51. }
  • 52. public void onMessage(Integer number ) {
  • 53. reply(2 * number );
  • 54. }
  • 55. public void onMessage(Money cash ) {
  • 56. System.out.println( &amp;quot;Received a donation &amp;quot; + cash );
  • 57. account .deposit( cash );
  • 58. }
  • 59. } </li></ul>
  • 60. Active Objects @ActiveObject class MyCounter { private int counter = 0 @ActiveMethod def incrementBy( int value) { println &amp;quot;Received an integer: $value &amp;quot; this .counter += value } }
  • 61. <ul>No more threads and locks </ul><ul>images. eachParallel { //concurrency agnostic code here } def myActor = actor { //concurrency agnostic code here } atomic { /*concurrency agnostic code here*/ } … </ul>
  • 62. <ul>Summary </ul><ul><li>Parallelism is not hard, multi-threading is
  • 63. Jon Kerridge, Napier University </li></ul>
  • 64. <ul><li>Questions?
  • 65. Find more at: </li><ul><li>http://gpars.codehaus.org
  • 66. http://www.jroller.com/vaclav
  • 67. http://twitter.com/vaclav_pech </li></ul></ul>

×