Threads & Concurrency: Lecture 23 - CS2110 - Fall 2018
Threads & Concurrency: Lecture 23 - CS2110 - Fall 2018
Threads & Concurrency: Lecture 23 - CS2110 - Fall 2018
--------------------------------
7:30. URHG01: aa2277 to gyz2
Suppose variable x is at
Memory location 800,
Instructions at 10
Machine language:
10: load register 1, 800
11: Add register 1, 2 Basic uniprocessor-CPU computer.
12: Store register 1, 800 Black lines indicate data flow, red
lines indicate control flow
From wikipedia
Part of Activity Monitor in Gries’s laptop
4
They reference the same data and can interfere with each
other, e.g. one process modifies a complex structure like a
heap while another is trying to read it
CS2110: we focus on two main issues:
Race conditions
Deadlock
Race conditions
14
Suppose x is initially 5
Thread t1 Thread t2
LOAD x ...
LOAD x
ADD 1 ADD 1
STORE x
STORE x
Each repeatedly
does this:
1. think
2. eat
What do they
eat?
spaghetti.
DEADLOCK!
Dining philosopher problem Simple solution to
deadlock:
20
Number the forks. Pick
up smaller one first
1. think
2. eat (2 forks)
5 4
eat is then:
pick up smaller fork
pick up bigger fork
pick up food, eat
3 put down bigger fork
1
put down smallerfork
2
Java: What is a Thread?
21
PrimeRun(long a, long b) {
this.a= a; this.b= b;
}
Doing I/O (e.g. read file, wait for mouse input, open file)
can cause thread to pause
Java has a form of locks associated with objects. When
threads lock an object, one succeeds at a time.
Background (daemon) Threads
32
The Windows and Linux and Apple O/S have kernel locking
features, like file locking