Professional Documents
Culture Documents
02 - Concurrency and - or in - Networking
02 - Concurrency and - or in - Networking
Networking
(or, in…)
ÉCOLE POLYTECHNIQUE –
CONCURRENCY AND
(OR, IN...) NETWORKING
TODAY’S OBJECTIVES
‣ Recall the essence of last weeks lecture.
‣ Understand concurrency
‣ Principles
‣ Primitives
‣ And, how it works in Java
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !57
CONCURRENCY AND
(OR, IN...) NETWORKING
AGENDA
‣ The Essence of Last Week:
Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives
(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
The
Big Bad
Internet
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
The
Big Bad
Internet
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
Big Bad
Internet
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
Big Bad
Internet Ethertype
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
… TCP UDP. …
Big Bad
Internet Ethertype
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
… TCP UDP. …
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
… TCP UDP. …
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
something.asp
script.cgi test.php filename.html
… IMAP HTTP SMTP IRC …
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
something.asp
script.cgi test.php filename.html
… IMAP HTTP SMTP IRC …
App specific
e.g. filenames, usernames, …
… TCP UDP. … Port Number
IMAP=143, HTTP=80, SMTP=25 ......
The IPv6 IPv4 Protocol Number
ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND
(OR, IN...) NETWORKING
192.168.23.45
something.asp
script.cgi test.php filename.html
… IMAP HTTP SMTP IRC …
App specific
e.g. filenames, usernames, …
… TCP UDP. … Port Number
IMAP=143, HTTP=80, SMTP=25 ......
The IPv6 IPv4 Protocol Number
ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype
The need to do this (identify, reach, ressources) efficiently remains a major design
challenge
(e.g., remains largely unresolved in IoT, or even in the Internet as per NAPT)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND
(OR, IN...) NETWORKING
… TCP UDP. …
Network
The need to do this (identify, reach, ressources) efficiently remains a major design
challenge
(e.g., remains largely unresolved in IoT, or even in the Internet as per NAPT)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND
(OR, IN...) NETWORKING
… TCP UDP. …
• IP Address (implies IPv4/IPv6)
• Protocol number Network
• Port number
The need to do this (identify, reach, ressources) efficiently remains a major design
challenge
(e.g., remains largely unresolved in IoT, or even in the Internet as per NAPT)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND
(OR, IN...) NETWORKING
… TCP UDP. …
• IP Address (implies IPv4/IPv6)
• Protocol number Network
• Port number
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND
(OR, IN...) NETWORKING
AGENDA
‣ The Essence of Last Week:
Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives
(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !62
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !62
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system
‣ May execute his/her actions at the same time
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !64
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !64
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !64
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !65
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !65
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
Each trader:
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !66
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C
Each trader:
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !66
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Sell A 2. Sell D
3. Sell A 3. Sell D
4. Sell B 4. Buy C
5. Sell B 5. Sell D
6. Sell B 6. Buy C
Each trader:
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !67
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Sell A 2. Sell D
3. Sell A 3. Sell D
4. Sell B 4. Buy C
5. Sell B 5. Sell D
6. Sell B 6. Buy C
Each trader:
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !67
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Sell D
2. Sell A 3. Sell D
3. Sell A 4. Buy C
5. Sell D
4. Sell B
5. Sell B 6. Buy C
6. Sell B
Each trader:
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !68
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
Coffee 2. Sell D
2. Sell A 3. Sell D
3. Sell A 4. Buy C
Buy Porche 5. Sell D
4. Sell B
Call mom
5. Sell B 6. Buy C
6. Sell B
Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !68
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !69
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !69
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !70
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !70
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions
‣ Trivially maintain local ordering of own actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !71
CONCURRENCY AND
(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions
‣ Trivially maintain local ordering of own actions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !71
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !72
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !72
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Sell A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !73
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Sell A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !73
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Buy A
Sell A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !73
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A
Buy A
Buy A
Sell A
Buy A
Buy A
Buy A
Buy A
Buy A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking Buy A !74
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A
Buy A
Buy A
Sell A
Buy A
Buy A
Buy A
Buy A
Buy A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking Buy A !74
CONCURRENCY AND
(OR, IN...) NETWORKING
Buy A
Buy A
Buy A
Sell A
Buy A
Buy A
Buy A
Buy A
Buy A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking Buy A !74
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !75
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !75
CONCURRENCY AND
(OR, IN...) NETWORKING
Each trader:
‣ State - causality with relevant actions of other traders
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !75
CONCURRENCY AND
(OR, IN...) NETWORKING
RECAPITULATING.....
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Possibly interleaved by other internal actions
‣ Is part of the same [stock exchange] system
‣ State - causality with relevant actions of other traders
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !76
CONCURRENCY AND
(OR, IN...) NETWORKING
RECAPITULATING.....
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Possibly interleaved by other internal actions
‣ Is part of the same [stock exchange] system
‣ State - causality with relevant actions of other traders
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !76
CONCURRENCY AND
(OR, IN...) NETWORKING
RECAPITULATING.....
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Possibly interleaved by other internal actions
‣ Is part of the same [stock exchange] system
‣ State - causality with relevant actions of other traders
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !76
CONCURRENCY AND
(OR, IN...) NETWORKING
AGENDA
‣ The Essence of Last Week:
Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives
(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)
Sell A
Sequential
actions
Executed in
Buy car specified
order
Buy B
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !78
Trader CONCURRENCY AND
(OR, IN...) NETWORKING
Sell A
Sequential
actions
Executed in
Buy car specified
order
Buy B
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !78
Trader Thread CONCURRENCY AND
(OR, IN...) NETWORKING
Executed in
Buy car specified a=b!
order
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !78
ThreadCONCURRENCY AND
(OR, IN...) NETWORKING
class Test{
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !79
CONCURRENCY AND
(OR, IN...) NETWORKING
Program
Address Space
Variables
Objects
foo()
bar()
Functions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !80
CONCURRENCY AND
(OR, IN...) NETWORKING
Program
Address Space
Variables
Objects
foo()
bar()
Functions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !80
CONCURRENCY AND
(OR, IN...) NETWORKING
Obviously, Pseudocode
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !81
CONCURRENCY AND
(OR, IN...) NETWORKING
Program
Address Space
Variables
Objects
foo()
bar()
Functions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !82
CONCURRENCY AND
(OR, IN...) NETWORKING
Program
Address Space
Variables
Objects
foo()
bar()
Functions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !82
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !83
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
or
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
or or
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
or or or or
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
or or or or or
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND
(OR, IN...) NETWORKING
Scheduling
A Thread:
An list of tasks, to be executed in sequential order.
A CPU:
or or A collection of 1-n execution units, eachor or tasks
executing orperfectly
sequentially
Scheduling:
Mapping of tasks from threads onto execution units
Observation:
Cannot assume order of execution of tasks to be known or even
deterministic.
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !85
CONCURRENCY AND
(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND
(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND
(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running
(e.g. it may not right now be mapped onto an
execution unit)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND
(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running
(e.g. it may not right now be mapped onto an
execution unit)
Not Runnable
Waiting for ressources
Waiting for some specific time to pass
Not waiting for something to happen in other threads
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND
(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running
(e.g. it may not right now be mapped onto an
execution unit)
Not Runnable
Waiting for ressources
Waiting for some specific time to pass
Not waiting for something to happen in other threads
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND
(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable Dead
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running
(e.g. it may not right now be mapped onto an
execution unit)
Not Runnable
Waiting for ressources
Waiting for some specific time to pass
Not waiting for something to happen in other threads
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
CONCURRENCY AND
(OR, IN...) NETWORKING
PRIMITIVE:
THREAD AND THREAD STATES
Thread t = new Thread (…)
t.start()
t.run()
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !87
New
A Thread: CONCURRENCY AND
An list of tasks, to be executed in sequential order. (OR, IN...) NETWORKING
Runnable Dead
Not Runnable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !88
New
Thread t = new Thread (...) CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !89
New
CONCURRENCY AND
Thread t = new Thread (...) (OR, IN...) NETWORKING
t.start()
Runnable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !90
New
CONCURRENCY AND
Thread t = new Thread (...) (OR, IN...) NETWORKING
t.start()
t.run() exits...
Runnable Dead
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !91
New
CONCURRENCY AND
Thread t = new Thread (...) (OR, IN...) NETWORKING
t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !92
New
CONCURRENCY AND
Thread t = new Thread (...) (OR, IN...) NETWORKING
t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running
Not Runnable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 93
New
CONCURRENCY AND
Thread t = new Thread (...) (OR, IN...) NETWORKING
t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running
sleep(timeout)
wait(timeout)
Not Runnable join(timeout)
wait()
join()
“Synchronization”
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 94
New
CONCURRENCY AND
Thread t = new Thread (...) (OR, IN...) NETWORKING
t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running
sleep(timeout)
wait(timeout)
Not Runnable join(timeout)
Block wait()
join()
Timed “Synchronization”
Wait
Wait
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 95
New
Thread t = new Thread (...) CONCURRENCY AND
(OR, IN...) NETWORKING
t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running
timeout expiration
join() returns
Not Runnable
Block notify() received
“Synchronization”
Timed
Wait
Wait
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 96
CONCURRENCY AND
(OR, IN...) NETWORKING
String s;
public ThreadFromRunnable(String t){
s = t;
}
public void run(){
while (true){ System.out.print(s); }
}
}
class RunnableExample{
t1.start();
t2.start();
}
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !97
CONCURRENCY AND
(OR, IN...) NETWORKING
PRIMITIVE:
BARRIER SYNCHRONISATION
s.join()
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !98
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !99
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !99
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !100
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
s.join()
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !100
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
s.join()
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !101
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
s.join()
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !102
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
s.join() s.run()
! exits
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !102
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
s.join() s.run()
exits
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !103
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
s.join() s.run()
exits
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !104
CONCURRENCY AND
(OR, IN...) NETWORKING
t s
Waiting Waiting
s.join() s.run()
exits
Primitive:
“barrier synchronization”
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !104
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !105
CONCURRENCY AND
(OR, IN...) NETWORKING
PRIMITIVE:
MUTUAL EXCLUSION (MUTEX)
synchronised {…}
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66
B: 24
x = 42 x = 0
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66
B: 24 B: 66
x = 42 x = 0 x = 0
a = x+24 b = x+24 x = 42
x = 0 x = 42 a = x+24
b = x+24 a = x+24 b = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
x = 0 x = 42 a = x+24 a = x+24
b = x+24 a = x+24 b = x+24 b = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
x = 0 x = 42 a = x+24 a = x+24
b = x+24 a = x+24 b = x+24 b = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !108
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66
B: 24
x = 42 x = 0
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
A: 66 A: 24
B: 66 B: 24
x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
A: 66 A: 24
B: 66 B: 24
x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A: 66 A: 24 A set of instructions, accessing one (or more) shared
B: 66 B: 24 variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].
x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A: 66 A: 24 A set of instructions, accessing one (or more) shared
B: 66 B: 24 variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].
x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24 “Synchronisation”
The act of ensuring the order of execution so as that
the performed computations produce a deterministic
result.
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A set of instructions, accessing one (or more) shared
variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].
“Synchronisation”
The act of ensuring the order of execution so as that
the performed computations produce a deterministic
result.
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66
B: 24
x = 42 x = 0
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A set of instructions, accessing one (or more) shared
variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND
(OR, IN...) NETWORKING
A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A set of instructions, accessing one (or more) shared
variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].
“Synchronisation”
The act of ensuring the order of execution so as that
the performed computations produce a deterministic
result.
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
x = 0 x = 42 a = x+24 a = x+24
b = x+24 a = x+24 b = x+24 b = x+24
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !111
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !112
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !113
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !113
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
if (!lock){ if (!lock){
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
} }
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
if (!lock){ if (!lock){
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
}
?
}
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
if (!lock){ if (!lock){
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
}
?
}
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
?
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !115
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
while (!lock){} while (!lock){}
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
?
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !115
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
while (!lock){} while (!lock){}
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
?
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !115
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !116
CONCURRENCY AND
(OR, IN...) NETWORKING
… and introduce the notion of a MUTEX, and its Java instantiation, directly.
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !116
CONCURRENCY AND
(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
while (!lock){} while (!lock){}
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
?
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !117
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
while (!lock){} while (!lock){}
lock = true; Special Property of Mutex: lock = true;
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock = false time I.e., no “both read true at the same time” lock = false
problem.
?
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !118
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.
?
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !119
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.
?
b.join() }
print (“A: “+a)
print (“B: “+b)
}
A: 66 A: 66 A: 24
B: 24 B: 66 B: 24
x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !119
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND
Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.
b.join() }
print (“A: “+a)
print (“B: “+b)
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND
Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.
b.join() }
print (“A: “+a) Only one thread executes
print (“B: “+b)
}
critical region at a time
Mutual Exclusion (MUTEX)
Mechanism for ensuring proper
order-of-execution of instructions
Obviously:
Critical Regions must be identified
And, protected by take()/release()
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND
(OR, IN...) NETWORKING
Th Val
Thread A: import java.lang.Thread; (b
ut is id,
.... class ThreadA extends Thread{ ab is, bu
,w
& e in
us ho t..
illu cl
str ud
public void run(){ Thread joinWith; ate ed
lock.take() public aThread (Thread t){ ing we
s “ it a
cla s it
Ja ver
joinWith = t; ss sh
ic ow
x = 42 } MU s
a = x+24 va ,
TE pre
Xe cis
s” el
lock.release()
b.join()
public void run(){
synchronized(MT.mutex){ MT.x = 42; MT.a = MT.x+24;}
ak y th
in
to e pr
in evi
pt ou
print (“A: “+a) try{ joinWith.join(); } catch (InterruptedException e){} hr s
ea sl
System.out.println("A: "+MT.a); ds ide
print (“B: “+b) ) s
System.out.println("B: "+MT.b);
} }
}
Thread B:
class ThreadB extends Thread{
.... public void run(){
public void run(){ synchronized(MT.mutex){ MT.x = 0; MT.b = MT.x+24;}
}
lock.take() }
x = 0
b = x+24 class MT{
lock.release() static int x, a, b; static Object mutex = new Object();
} public static void main(String[] args){
Thread t1 = new Thread(new ThreadB());
Thread t2 = new Thread(new ThreadA(t1));
t1.start(); t2.start();
}
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER
synchronized(s){ synchronized(t){
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER
synchronized(s){ synchronized(t){
synchronized(t){ synchronized(s){
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER
synchronized(s){ synchronized(t){
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER
synchronized(s){ synchronized(t){
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER
synchronized(s){ synchronized(t){
} // releases t } // releases s
} // releases s } // release t
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER
synchronized(s){ synchronized(t){
} // releases t } // releases s
} // releases s } // release t
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
CONCURRENCY AND
(OR, IN...) NETWORKING
Th Val
Thread A: import java.lang.Thread; (b
ut is id,
.... class ThreadA extends Thread{ ab is, bu
,w
& e in
us ho t..
illu cl
str ud
public void run(){ Thread joinWith; ate ed
lock.take() public aThread (Thread t){ ing we
s “ it a
cla s it
Ja ver
joinWith = t; ss sh
ic ow
x = 42 } MU s
a = x+24 va ,
TE pre
Xe cis
s” el
lock.release()
b.join()
public void run(){
synchronized(MT.mutex){ MT.x = 42; MT.a = MT.x+24;}
ak y th
in
to e pr
in evi
pt ou
print (“A: “+a) try{ joinWith.join(); } catch (InterruptedException e){} hr s
ea sl
System.out.println("A: "+MT.a); ds ide
print (“B: “+b) ) s
System.out.println("B: "+MT.b);
} }
}
Thread B:
class ThreadB extends Thread{
.... public void run(){
public void run(){ synchronized(MT.mutex){ MT.x = 0; MT.b = MT.x+24;}
}
lock.take() }
x = 0
b = x+24 class MT{
lock.release() static int x, a, b; static Object mutex = new Object();
} public static void main(String[] args){
Thread t1 = new Thread(new ThreadB());
Thread t2 = new Thread(new ThreadA(t1));
t1.start(); t2.start();
}
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND
(OR, IN...) NETWORKING
Th Val
import java.lang.Thread; (b
ut is id,
class ThreadA extends Thread{
,w
& e in ab is, bu
us ho t..
illu cl
str ud
Thread joinWith; ate ed
public aThread (Thread t){
s “ it a
cla s it ing we
Ja ver
ss sh
joinWith = t; ic ow
MU s
} TE pre
Xe cisva ,
s” el
public void run(){
ak y th
in
to e pr
synchronized(MT.mutex){ MT.x = 42; MT.a = MT.x+24;} in evi
pt ou
try{ joinWith.join(); } catch (InterruptedException e){} hr s
ea sl
System.out.println("A: "+MT.a); ds ide
) s
System.out.println("B: "+MT.b);
}
}
class MT{
static int x, a, b; static Object mutex = new Object();
public static void main(String[] args){
Thread t1 = new Thread(new ThreadB());
Thread t2 = new Thread(new ThreadA(t1));
t1.start(); t2.start();
}
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND
(OR, IN...) NETWORKING
Mutex / Condition
Variable/Semaphore
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND
(OR, IN...) NETWORKING
Mutex / Condition
Variable/Semaphore
Shared Variable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND
(OR, IN...) NETWORKING
Mutex / Condition
Variable/Semaphore
Shared Variable
Critical Regions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND
(OR, IN...) NETWORKING
Mutex / Condition
Variable/Semaphore
Shared Variable
Disjoint from
Critical Regions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND
(OR, IN...) NETWORKING
Disjoint from
Methods
Critical Regions
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND
(OR, IN...) NETWORKING
Monitor
mutex
Object
Condition Variable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND
(OR, IN...) NETWORKING
Object
Condition Variable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND
(OR, IN...) NETWORKING
Condition Variable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND
(OR, IN...) NETWORKING
Condition Variable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND
(OR, IN...) NETWORKING
class ExampleOfMonitor{
private int mySharedValue;
public synchronized void set (int i){
mySharedValue=i;
}
public synchronized int get(){
return mySharedValue;
}
public synchronized void reset(){
mySharedValue=0;
} Condition Variable
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !127
CONCURRENCY AND
(OR, IN...) NETWORKING
PRIMITIVE:
CONDITION VARIABLES
wait()
notify()
notifyAll()
Queue
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND
(OR, IN...) NETWORKING
Queue
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND
(OR, IN...) NETWORKING
Critical section
MUTEX?
Queue
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND
(OR, IN...) NETWORKING
Queue
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND
(OR, IN...) NETWORKING
Queue
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND
(OR, IN...) NETWORKING
Queue
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
queue.put(...)
lock.release()
}
Queue
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !130
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
if (!queue.full()){
queue.put(...)
}
lock.release() Queue
}
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !131
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
while (!queue.full()){
queue.put(...)
}
lock.release() Queue
}
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !132
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer:
.... Mutex lock = new Mutex()
public void run(){ Queue will never be emptied, Critical section
lock.take() lock never released
while (!queue.full()){
queue.put(...)
}
lock.release() Queue
}
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !132
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
while (queue.full()){
queueNotFull.wait(lock)
}
queue.put(...) Queue
lock.release()
}
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !133
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer:
.... Mutex lock = new Mutex()
public void run(){ “Temporarily” relinquish lock Critical section
lock.take() until a get() has happened?
while (queue.full()){
queueNotFull.wait(lock)
}
queue.put(...) Queue
lock.release()
}
put(...) get()
Producer Consumer
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !133
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !134
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}
Producer:
.... Mutex lock = new Mutex()
public void run(){
lock.take()
while (queue.full()){ class ConditionVariable{
queueNotFull.wait(lock) private Queue suspendedThreads = new Queue()
}
queue.put(...) public void wait(Mutex s){
// add calling thread to suspendedThreads
lock.release() s.unlock()
} // tell scheduler that thread is blocked
s.lock()
}
Consumer:
.... public void notify(){
Thread t = suspendedThreads.first();
public void run(){ // tell scheduler that t is unblocked
lock.take() }
queue.get()
queueNotFull.notify(lock)
lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !136
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !137
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock)} queueNotFull.notify(lock)
queue.put(...) lock.release()
lock.release() }
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !138
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){
queueNotFull.wait(lock)}
queue.put(...)
queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained
• Must ensure that shared data is consistent !
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained
• Must ensure that shared data is consistent !
• wait() instructs scheduler to ignore waiting thread
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained
• Must ensure that shared data is consistent !
• wait() instructs scheduler to ignore waiting thread
• Note that notify() when no waiting threads are lost
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND
(OR, IN...) NETWORKING
Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain: Te
no rm
ab rma ino
• Condition Variables associated with a lock ou lly l o
t “ , i gy
• Permit temporarily relinquishing lock - to be regained In
wa java
it/ , t
co t’s A
nd w le
iti ait rt:
on /s
• Must ensure that shared data is consistent ! no he
tif eq
y uiv
va ign
ria al
ble w h
• wait() instructs scheduler to ignore waiting thread ale
nt
s” e n
tal
• Note that notify() when no waiting threads are lost co
nc
ep
ts
kin
g
ar
ec
all
ed
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND
(OR, IN...) NETWORKING
class PizzaTest{
public static void main (String[] args) throws Exception{
House ThomasHouseFridayNight = new House();
Thread Thomas = new Thread(new PizzaEater(ThomasHouseFridayNight));
Thread PizzaHut = new Thread(new PizzaPlace(ThomasHouseFridayNight));
Thomas.start() ; PizzaHut.start();
}
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND
(OR, IN...) NETWORKING
class PizzaTest{
public static void main (String[] args) throws Exception{
House ThomasHouseFridayNight = new House();
Thread Thomas = new Thread(new PizzaEater(ThomasHouseFridayNight));
Thread PizzaHut = new Thread(new PizzaPlace(ThomasHouseFridayNight));
Thomas.start() ; PizzaHut.start();
}
}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND
(OR, IN...) NETWORKING
AGENDA
‣ The Essence of Last Week:
Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives
(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)
WHY THREADS?
WHY CONCURRENT PROGRAMMING?
‣ Exploit multiple execution units
(CPUs, cores)
‣ Software Engineering:
‣ Blocking System Calls
e.g. java.io.InputStream.read()
‣ Modularity & Architecture
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !142
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !143
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !143
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !144
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !144
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !144
CONCURRENCY AND
(OR, IN...) NETWORKING
cat /etc/shadow |
grep "werner" |
john - |
mailx -s "BW’s password" tout-le-monde-sur-site@polytechnique.fr
Files
Worker 2
Master Databases
Disks
Worker n
Special Devices
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !146
CONCURRENCY AND
(OR, IN...) NETWORKING
Files
Worker 2
Master Databases
Disks
Worker n
Special Devices
Request Dispatch
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !146
CONCURRENCY AND
(OR, IN...) NETWORKING
Files
Worker 2
Master Databases
Disks
Worker n
Special Devices
Request Dispatch
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !146
CONCURRENCY AND
(OR, IN...) NETWORKING
Files
Worker 2
Master Databases
Disks
Worker n
Special Devices
Worker 1
Worker 2 Files
Master Databases
Disks
Worker n
Special Devices
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !147
CONCURRENCY AND
(OR, IN...) NETWORKING
Stage 1 Stage 2 Stage n
Worker 1
Worker 2 Files
Master Databases
Disks
Worker n
Special Devices
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !147
CONCURRENCY AND
(OR, IN...) NETWORKING
Stage 1 Stage 2 Stage n
Worker 1
Worker 2 Files
Master Databases
Disks
Worker n
Special Devices
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !147
CONCURRENCY AND
(OR, IN...) NETWORKING
Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded
(if not, mark that it will be)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND
(OR, IN...) NETWORKING
Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded
(if not, mark that it will be)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND
(OR, IN...) NETWORKING
Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded
(if not, mark that it will be)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND
(OR, IN...) NETWORKING
Worker 1
URL
URL
URL
…
Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded
(if not, mark that it will be)
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND
(OR, IN...) NETWORKING
‣ Primitives
‣ Threads
‣ MUTEXes (synchronised …)
‣ Condition Variables (wait/notify)
‣ Monitors (Object w/ one mutex + one condition variable)
‣ Barrier Synchronisation (join)
‣ Architectures
‣ Pipelining
‣ Master-Worker
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !149
CONCURRENCY AND
(OR, IN...) NETWORKING
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !150
https://www.polytechnique.edu/