introduction to concurrent programming - chalmers · states of a java thread ready new;start()...
TRANSCRIPT
![Page 1: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/1.jpg)
Introduction to concurrent programming
Lecture 1 of TDA383/DIT390 (Concurrent Programming)
Carlo A. Furia
Chalmers University of Technology – University of GothenburgSP3 2016/2017
![Page 2: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/2.jpg)
Today’s menu
A motivating example
Why concurrency?
Basic terminology and abstractions
Java threads
Traces
1 / 27
![Page 3: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/3.jpg)
A motivating example
![Page 4: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/4.jpg)
As simple as counting to two
We illustrate the challenges introduced by concurrent programmingon a simple example: a counter modeled by a Java class.
• First, we write a traditional, sequential version
• Then, we introduce concurrency and. . . run into trouble!
2 / 27
![Page 5: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/5.jpg)
Sequential counter
public class Counter {
private int counter = 0;
// increment counter by one
public void run() {
int cnt = counter;
counter = cnt + 1;
}
// current value of counter
public int counter() {
return counter;
}
}
public class SequentialCount {
public static
void main(String[] args) {
Counter counter = new Counter();
counter.run(); // increment once
counter.run(); // increment twice
// print final value of counter
System.out.println(
counter.counter());
}
}
• What is printed by running: java SequentialCount?
• May the printed value change in different reruns?3 / 27
![Page 6: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/6.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 7: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/7.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6
•
•
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 8: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/8.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6
•
•
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 9: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/9.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6
•
•
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 10: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/10.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6•
•
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 11: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/11.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6•
•
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 12: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/12.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6•
•
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 13: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/13.jpg)
Modeling sequential computation
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 }
counter.run(); // first call: steps 1-3
counter.run(); // second call: steps 4-6
# LOCAL STATE OBJECT STATE
1 pc : 6 cnt : ⊥ counter : 02 pc : 7 cnt : 0 counter : 03 pc : 8 cnt : 0 counter : 14 pc : 6 cnt : ⊥ counter : 15 pc : 7 cnt : 1 counter : 16 pc : 8 cnt : 1 counter : 27 done counter : 2
4 / 27
![Page 14: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/14.jpg)
Adding concurrency
Now, we revisit the example by introducing concurrency:
Each of the two calls to method run can be executed in parallel
In Java, this is achieved by using threads. Do not worry about thedetails of the syntax for now, we will explain it later.
The idea is just that:
• There are two independent execution units (threads) t and u
• Each execution unit executes run on the same counter object
• We have no control over the order of execution of t and u
5 / 27
![Page 15: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/15.jpg)
Concurrent counter
public class CCounter
extends Counter
implements Runnable
{
// threads
// will execute
// run()
}
public class ConcurrentCount {
public static void main(String[] args) {
CCounter counter = new CCounter();
// threads t and u, sharing counter
Thread t = new Thread(counter);
Thread u = new Thread(counter);
t.start(); // increment once
u.start(); // increment twice
try { // wait for t and u to terminate
t.join(); u.join(); }
catch (InterruptedException e)
{ System.out.println("Interrupted!"); }
// print final value of counter
System.out.println(counter.counter());
} }
• What is printed by running: java ConcurrentCount?• May the printed value change in different reruns?
6 / 27
![Page 16: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/16.jpg)
What?!
$ javac Counter.java CCounter.java ConcurrentCount.java
$ java ConcurrentCount.java
2
$ java ConcurrentCount.java
2
...
$ java ConcurrentCount.java
1
$ java ConcurrentCount.java
2
The concurrent version of counter occasionally prints 1 instead of theexpected 2. It seems to do so unpredictably.
7 / 27
![Page 17: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/17.jpg)
What?!
$ javac Counter.java CCounter.java ConcurrentCount.java
$ java ConcurrentCount.java
2
$ java ConcurrentCount.java
2
...
$ java ConcurrentCount.java
1
$ java ConcurrentCount.java
2
The concurrent version of counter occasionally prints 1 instead of theexpected 2. It seems to do so unpredictably.
Welcome to concurrent programming!
7 / 27
![Page 18: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/18.jpg)
8 / 27
![Page 19: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/19.jpg)
Why concurrency?
![Page 20: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/20.jpg)
Reasons for using concurrency
Why do we need concurrent programming in the first place?
abstraction: separating different tasks, without worrying about whento execute them (example: download files from twodifferent websites)
responsiveness: providing a responsive user interface, withdifferent tasks executing independently (example:browse the slides while downloading your email)
performance: splitting complex tasks in multiple units, and assigneach unit to a different processor (example: computeall prime numbers up to 1 billion)
9 / 27
![Page 21: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/21.jpg)
Concurrency vs. parallelism
In this course we will mostly use concurrency and parallelism assynonyms. However, they refer to similar but different concepts:
concurrency: nondeterministic composition of independentlyexecuting units (logical parallelism)
parallelism: efficient execution of fractions of a complex task onmultiple processing units (physical parallelism)
• You can have concurrency without physical parallelism: operatingsystems running on single-processor single-core systems
• Parallelism is mainly about speeding up computations by takingadvantage of redundant hardware
10 / 27
![Page 22: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/22.jpg)
Moore’s law and its end
The spectacular advance of computing in the last 60+ years has beendriven by Moore’s law:
The density of transistors in integrated circuitsdoubles approximately every 2 years
11 / 27
![Page 23: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/23.jpg)
Moore’s Law in January 2017
12 / 27
![Page 24: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/24.jpg)
Concurrency everywhere
The end of Moore’s law is having a major impact on the practice ofprogramming:
• before: CPUs get faster without significant architectural changes• program as usual, and wait for your program to run faster• concurrent programming is a niche skill (for operating systems,
databases, high-performance computing)
• now: CPUs do not get faster but add more and more parallelcores
• program with concurrency in mind, otherwise your programsremain slow
• concurrent programming is pervasive
Very different systems all require concurrent programming:
• desktop PCs
• smart phones
• video-games consoles
• embedded systems
• the Raspberry Pi
• cloud computing13 / 27
![Page 25: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/25.jpg)
Amdahl’s law: concurrency is no free lunch
We have n processors that can run in parallel. How much speedupcan we achieve?
speedup =sequential execution time
parallel execution time
Amdahl’s law shows that the impact of introducing parallelism islimited by the fraction p of a program that can be parallelized:
maximum speedup =1
(1− p)︸ ︷︷ ︸sequential part
+ p/n︸︷︷︸parallel part
14 / 27
![Page 26: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/26.jpg)
Amdahl’s law: examples
maximum speedup =1
(1− p)︸ ︷︷ ︸sequential part
+ p/n︸︷︷︸parallel part
With n = 10 processors, how close can we get to a 10x speedup?
% SEQUENTIAL % PARALLEL MAX SPEEDUP
20% 80% 3.5710% 90% 5.26
1% 99% 9.17
With n = 100 processors, how close can we get to a 100x speedup?
% SEQUENTIAL % PARALLEL MAX SPEEDUP
20% 80% 4.8110% 90% 9.17
1% 99% 50.25
15 / 27
![Page 27: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/27.jpg)
Basic terminology andabstractions
![Page 28: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/28.jpg)
Processes
A process is an independent unit of execution – the abstraction of arunning sequential program:
• identifier
• program counter
• memory space
The runtime/operating system schedules processes for execution onthe available processors:
CPU1 running process P3 CPU2 running process P2
Process P1 is waiting scheduler
16 / 27
![Page 29: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/29.jpg)
Processes
A process is an independent unit of execution – the abstraction of arunning sequential program:
• identifier
• program counter
• memory space
The runtime/operating system schedules processes for execution onthe available processors:
CPU1 running process P3 CPU2 running process P2
Process P1 is waiting scheduler
suspend
resume
16 / 27
![Page 30: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/30.jpg)
Processes
A process is an independent unit of execution – the abstraction of arunning sequential program:
• identifier
• program counter
• memory space
The runtime/operating system schedules processes for execution onthe available processors:
CPU1 running process P3
scheduler
CPU2 running process P1
Process P2 is waiting
16 / 27
![Page 31: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/31.jpg)
Process states
The scheduler is the system unit in charge of setting process states:
ready: ready to be executed, but not allocated to any CPU
blocked: waiting for an event to happen
running: running on some CPU
readynew
blocked
running terminate
resume
suspend
waitevent
17 / 27
![Page 32: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/32.jpg)
Threads
A thread is a lightweight process – an independent unit of executionon the same program space:
• identifier
• program counter
• memory• local memory, separate for
each thread• global memory, shared with
other threads
shared memory
Thread T1 · · · Thread Tn
T1’s local memory Tn’s local memory
In practice, the difference between processes and threads is fuzzyand implementation dependent. Normally in this course:
processes: executing units that do not share memory (in Erlang)
threads: executing units that share memory (in Java)
18 / 27
![Page 33: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/33.jpg)
Shared memory vs. message passing
Shared memory models:
• communication by writing toshared memory
• e.g. multi-core systems
shared memory
Thread T1 · · · Thread Tn
Distributed memory models:
• communication by messagepassing
• e.g. distributed systems
memory
· · ·
Process P1
memory
Process Pn
message
19 / 27
![Page 34: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/34.jpg)
Java threads
![Page 35: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/35.jpg)
Java threads
Two ways to build multi-threaded programs in Java:
• inherit from class Thread, override method run
• implement interface Runnable, implement method run
public class CCounter
implements Runnable
{
// thread’s computation:
public void run() {
int cnt = counter;
counter = cnt + 1;
}
}
CCounter c = new CCounter();
Thread t = new Thread(c);
Thread u = new Thread(c);
t.start();
u.start();
20 / 27
![Page 36: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/36.jpg)
States of a Java thread
ready
new; start()
blocked/waiting
running
terminate
resume
suspend
wait()
sleep()
event
Resuming and suspending is doneby the JVM scheduler, outside theprogram’s control
For a thread object t:
• t.start(): the thread is readyfor execution
• t.sleep(n): block the threadfor n milliseconds (correcttiming depends on JVMimplementation)
• t.wait(): block the threaduntil an event occurs
• t.join(): block the currentthread until t terminates
21 / 27
![Page 37: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/37.jpg)
Thread execution model
shared objects
thread t1 · · · thread tn
t1’s local memory tn’s local memory
Shared vs. thread-local memory:
• shared objects: the object onwhich the thread operate, andall reachable objects
• local memory: local variables,and special thread-localattributes
Threads proceed asynchronously, so they have to coordinate withother threads accessing the same shared objects.
22 / 27
![Page 38: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/38.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 39: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/39.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
••
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 40: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/40.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
••
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 41: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/41.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } } •
•
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 42: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/42.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
•
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 43: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/43.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
•
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 44: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/44.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } } •
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 45: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/45.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 46: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/46.jpg)
One possible execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 8 cntt : 0 pcu : 6 cntu : ⊥ counter : 14 done pcu : 6 cntu : ⊥ counter : 15 done pcu : 7 cntu : 1 counter : 16 done pcu : 8 cntu : 1 counter : 27 done done counter : 2
23 / 27
![Page 47: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/47.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 48: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/48.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
••
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 49: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/49.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
••
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 50: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/50.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
••
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 51: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/51.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
••
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 52: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/52.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } } •
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 53: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/53.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 54: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/54.jpg)
One alternative execution of the concurrent counter
1 public class CCounter implements Runnable {
2 int counter = 0; // shared object state
3
4 // thread’s computation:
5 public void run() {
6 int cnt = counter;
7 counter = cnt + 1;
8 } }
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
24 / 27
![Page 55: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/55.jpg)
Traces
![Page 56: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/56.jpg)
Traces
# t’S LOCAL u’S LOCAL SHARED
1 pct : 6 cntt : ⊥ pcu : 6 cntu : ⊥ counter : 02 pct : 7 cntt : 0 pcu : 6 cntu : ⊥ counter : 03 pct : 7 cntt : 0 pcu : 7 cntu : 0 counter : 04 pct : 7 cntt : 0 pcu : 8 cntu : 0 counter : 15 pct : 8 cntt : 0 done counter : 16 done done counter : 1
The sequence of states gives an execution trace of the concurrentprogram. A trace is an abstraction of concrete executions:
• atomic/linearized
• complete
• interleaved
25 / 27
![Page 57: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/57.jpg)
Trace abstractions
cnt = counter counter = cnt + 1
cnt = counter counter = cnt + 1
thread t
thread u
counter 0 1 1
trace states: 1 2 3 4 5 6
atomic/linearized: the effects of each thread appear as if theyhappened instantaneously, when the trace snapshot istaken, in the thread’s sequential order
complete: the trace include all intermediate atomic states
interleaved: the trace is an interleaving of each thread’s linear trace(in particular, no simultaneity)
26 / 27
![Page 58: Introduction to concurrent programming - Chalmers · States of a Java thread ready new;start() blocked/ waiting running terminate resume suspend wait() sleep() event Resuming and](https://reader035.vdocuments.net/reader035/viewer/2022081607/5f01e9657e708231d401a51d/html5/thumbnails/58.jpg)
Abstraction of concurrent programs
When convenient, we will use an abstract notation for multi-threadedapplications, which is similar to the pseudo-code used in Ben-Ari’sbook but uses Java syntax.
int counter = 0;
thread t thread u
int cnt; int cnt;
1 cnt = counter;
2 counter = cnt + 1;
cnt = counter; 1
counter = cnt + 1; 2
shared memory
local memory
code
Each line of code includes exactly one instruction that can beexecuted atomically:
• atomic statement ' single read or write to global variable• precise definition is tricky in Java, but we will learn to avoid pitfalls
27 / 27