Top Banner
Concurrency for dummies and for real people too By Azrul HasniMad Isa ([email protected])
43
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Concurrency for dummies

Concurrency for dummies

and for real people too

By

Azrul Hasni Mad Isa

([email protected])

Page 2: Concurrency for dummies

About me

• Senior Software

Architect at Experian

Decision Analytics

• 10 years of experience in • 10 years of experience in

IT and such

• Graduated from France

(INSA)

Page 3: Concurrency for dummies

About Experian

• In Cyberjaya– Other R&D Centres in

Nottingham, Monaco, Washington

• Financial analytics, market analyticsanalytics

• 200+ people and growing

• We use Scrum

• We are looking for people– Java, PHP, Perl, C#

– Dev, QA, Architects, Team lead

Page 4: Concurrency for dummies

Introduction

• What is concurrency

Concurrency is a property of systemsin which several computations areexecuting simultaneously, andpotentially interacting with each otherpotentially interacting with each other

-- Wikipedia

Page 5: Concurrency for dummies

Introduction

• Why concurrent applications are important

Page 6: Concurrency for dummies

Introduction

• Our business processes are getting more

complex!

Page 7: Concurrency for dummies

Business case for concurrency

• E-commerce

Buy Product Online

Parallel SplitUser

Parallel Split

“Your request is

being process”

Charge Credit Card

Check availability

Reserve product

Email confirmation

Page 8: Concurrency for dummies

Business case for concurrency

• Financial – credit verification

Apply Credit CardCCRIS

Credential Verification Equifax

Experian

Approve/Not

User

Decision making

Page 9: Concurrency for dummies

Business case for concurrency

• LogisticsArrival of merchandises

For every merchandise

Parallel Split

Custom & Port authority

Warehousing Quality check

Transportation

Page 10: Concurrency for dummies

Business case for concurrency

• Many others …

Page 11: Concurrency for dummies

Model for concurrency management

• Actors

• Agents

• Dataflow

• Stm• Stm

• Data paralellism

– Concurrent collections

– Asynchronous functions (closure)

– Fork/Join

Page 12: Concurrency for dummies

Model for concurrency management

• Actors

• Agents

• Dataflow

• Stm• Stm

• Data paralellism

– Concurrent collections

– Asynchronous functions (closure)

– Fork/Join

Page 13: Concurrency for dummies

GPARS

Page 14: Concurrency for dummies

GPars

• Gpars is a concurrency

management library for

Groovy and Java

• Allow running models • Allow running models

presented above

• http://gpars.codehaus.org/

Page 15: Concurrency for dummies

ACTORS

Page 16: Concurrency for dummies

Actors

• Why couldn’t they call ‘em actresses instead?

• A body of code that:

– Receive a message asynchronously

– React to a message if it can – React to a message if it can

– Queue a received message if it’s busy

– Asynchronous messaging (JMS)

Page 17: Concurrency for dummies

Actor

• Synchronous messaging = immediate reply

The Nora Elena actress is soo cute!actress is soo cute!

Why You !! You !!... I’ll kill you!!

Page 18: Concurrency for dummies

Actor• Asynchronous messaging = non-immediate

reply

– We can do other things while waiting

Could you please make me a drink? …make me a drink? …

I’m going to ponderabout the meaning of life in the meantime

Page 19: Concurrency for dummies

Actor• Asynchronous messaging = non-immediate

reply

– We can do other things while waiting

Could you please make me a drink? …

I was watching TV. Go make the drink

yourself!

2 hours later

make me a drink? …

I’m going to ponderabout the meaning of life in the meantime

Page 20: Concurrency for dummies

Actors

Queue Queue

OneActor AnotherActor

“sending a

message”

Page 21: Concurrency for dummies

Actors

Queue Queue“reply to a

message”

OneActor AnotherActor

Page 22: Concurrency for dummies

Actors

• Example:class MyActor extends DefaultActor {

Actor anotherActor

void act() {

loop {

anotherActor.send “Hello” //send message to someone

react { String message -> //message could be any POGOreact { String message -> //message could be any POGO

…//do something

reply someReply //some reply is any POGO

terminate() //kill the actor *gasp!*

}

}

}

}

}

Page 23: Concurrency for dummies

Actors

• Two types

– Stateless : DynamicDispatchActor

– Stateful: DefaultActor

• Actors are can share a common • Actors are can share a common

threadpool

• Threadpool + statelessness =

SCALABILITY

• No remote actors yet �

Page 24: Concurrency for dummies

Actor

• Some methods

– loop

• Infinite loop on the actor

– react– react

• React on message received

– send

• Send a message

– reply

• Reply to a message

Page 25: Concurrency for dummies

Actor

• Cool stuff

– Loop counting

– Code in actors are guaranteed to be thread safe

– Life cycle methods– Life cycle methods

– Dealing with unsent messages

• Business Process Compensation

– Remote actors – coming

Page 26: Concurrency for dummies

DATAFLOW

Page 27: Concurrency for dummies

Dataflow

• First introduced in a language called Oz

– Composed of tasks

– Each task has input and output

– A task is run as soon as all its input are available– A task is run as soon as all its input are available

Page 28: Concurrency for dummies

Dataflow

Task

print z

Task

z = a+b

Not executed

Task

a=20

Task

b=32

z = a+b

Page 29: Concurrency for dummies

Dataflow

Task

print z

Task

z = a+b

Not executed

Task

print z

Task

z = a+b

First round Executed

Task

a=20

Task

b=32

z = a+b

Task

a=20

Task

b=32

z = a+b

No input needed

Page 30: Concurrency for dummies

Dataflow

Task

print z

Task

z = a+b

Not executed

Task

print z

Task

z = a+b

First round

Task

print z

Task

z = a+b

Second round Executed

Task

a=20

Task

b=32

z = a+b

Task

a=20

Task

b=32

z = a+b

Task

a=20

Task

b=32

z = a+b

Input, a and b,becomes available

Page 31: Concurrency for dummies

Dataflow

Task

print z

Task

z = a+b

Not executed

Task

print z

Task

z = a+b

First round

Task

print z

Task

z = a+b

Second round

Task

print z

Task

z = a+b

Third round

Input, z,becomes available

Task

a=20

Task

b=32

z = a+b

Task

a=20

Task

b=32

z = a+b

Task

a=20

Task

b=32

z = a+b

Task

a=20

Task

b=32

z = a+b

Page 32: Concurrency for dummies

Dataflow: code

final def x = new DataFlowVariable()

final def y = new DataFlowVariable()

final def z = new DataFlowVariable()

task {

z << x.val + y.val

}

task {task {

x << 10

}

task {

y << 5

}

println "Result: ${z.val}"

Page 33: Concurrency for dummies

Dataflow: principles

• Create a dataflow variable– The variable can be written once

only

• Read it– If it has no value yet, the program

will wait for the new value while will wait for the new value while executing other tasks

• Write to it– The moment a variable is written

(or bounded) any task waiting for this variable will be reactivated

Page 34: Concurrency for dummies

Dataflow: beauty

• “Declarative” concurrency

• Clean code

– Business logic is much more obvious

– Business logic can be sequential– Business logic can be sequential

• Flow is deterministic

• If no deadlock in unit test, it is guaranteed

that there will be no deadlocks in production.

Page 35: Concurrency for dummies

Dataflow in business processApply Credit Card

CCRIS Equifax Experian

Parallel split

Approve/Not

Decision making

Parallel join

Page 36: Concurrency for dummies

Dataflow in business processApply Credit Card

CCRIS Equifax Experian

Parallel splitDo business users really

think in Parallel

executions?

Approve/Not

Decision making

Parallel join

executions?

Page 37: Concurrency for dummies

Dataflow in business process

I want to verify user credentials against

CCRIS, Experian and Equifax

You need parallelprocessing

Business

stakeholder

Solution architect

Page 38: Concurrency for dummies

Dataflow in business process

I want to verify user credentials against

CCRIS, Experian and Equifax

You need parallelprocessing

Suggestion by the solution architect

Business

stakeholder

Solution architect

Page 39: Concurrency for dummies

Dataflow in business process

• Meanwhile, in a parallel universe … pun intended ☺

I want to verify user credentials against

CCRIS, Experian and Equifax

Apply Credit Card

CCRIS

Equifax

OK?Dataflow Tasks

Business

stakeholder

Solution architect

Equifax

Experian

Approve/Not

Decision making

Page 40: Concurrency for dummies

Dataflow in business process

• Meanwhile, in a parallel universe

Hey, I totally get that!

I totally want to give you more business!

Parallel execution is

done automatically

by GParsDataflow

Would you like a pony too?

Business

stakeholder

Solution architect

Page 41: Concurrency for dummies

Dataflow in business process

• Meanwhile, in a parallel universe

Yes please

Solution architect

Page 42: Concurrency for dummies

Conclusion

• Actor

– Asynchronous messaging

• Dataflow

– “Declarative” concurrency– “Declarative” concurrency

Page 43: Concurrency for dummies

Conclusion

• What we see is merely the tip of the iceberg

• Explore yourself

– http://gpars.codehaus.org/

– http://groovy.codehaus.org/– http://groovy.codehaus.org/

– http://en.wikipedia.org/wiki/Actor_model

– http://en.wikipedia.org/wiki/Dataflow

• My contact: [email protected]