About me
['Passionate programmer',
'Concurrency enthusiast',
'GPars lead',
'Developer/technology evangelist @ JetBrains'
].eachParallel {say it}
http://www.jroller.com/vaclav
http://twitter.com/vaclav_pech
Slide 3
Slide 3 text
We're all parallel now
Use them or leave them!
Slide 4
Slide 4 text
No content
Slide 5
Slide 5 text
# of cores
Slide 6
Slide 6 text
# of cores
Today
Soon
Slide 7
Slide 7 text
Dealing with threads sucks!
public class Counter {
private static long count = 0;
public Counter() {
count++;
}
}
Slide 8
Slide 8 text
Dealing with threads sucks!
public class Counter {
private static long count = 0;
public Counter() {
synchronized (this) {
count++;
}
}
}
Slide 9
Slide 9 text
Dealing with threads sucks!
public class Counter {
private static long count = 0;
public Counter() {
synchronized (this.getClass()) {
count++;
}
}
}
Slide 10
Slide 10 text
Dealing with threads sucks!
public class ClickCounter implements ActionListener {
public ClickCounter(JButton button) {
button.addActionListener(this);
}
public void actionPerformed(final ActionEvent e) {
...
}
}
Actors
Processes with a mail-box
Share no data
Communicate by sending messages
Use a thread-pool
Slide 29
Slide 29 text
Stateless Actors (pure Java)
class MyActor extends DynamicDispatchActor {
Account account = ...
public void onMessage(String msg) {
String encripted = encrypt(msg);
reply(encripted);
}
public void onMessage(Integer number) {
reply(2 * number);
}
public void onMessage(Money cash) {
System.out.println("Received a donation " + cash);
account.deposit(cash);
}
}
Slide 30
Slide 30 text
Active Objects
@ActiveObject
class MyCounter {
private int counter = 0
@ActiveMethod
def incrementBy(int value) {
println "Received an integer: $value"
this.counter += value
}
}
Slide 31
Slide 31 text
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
int hash(String text) {…}
Promise hash(Promise | String text) {
1.Return a Promise for the result
2.Wait (non-blocking) for the text param
3.Call the original hash()
4.Bind the result
}
Slide 37
Slide 37 text
Composing async functions
Combine functions as usual
Parallelism is detected automatically
Slide 38
Slide 38 text
Dataflow Concurrency
No race-conditions
No live-locks
Deterministic deadlocks
Completely deterministic programs
BEAUTIFUL code
(Jonas Bonér)
Slide 39
Slide 39 text
Dataflow Variables / Promises
main task2 task3
x
y
z
task1