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,260

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

×