Академический Документы
Профессиональный Документы
Культура Документы
Agenda
Concurrency - definition and motivation
Processes and Threads in JAVA
Thread safe programming: Safety and Liveliness
Safe concurrent design
SWING and Threads in NinJo
Concurrent Execution
A
Concurrency
B
C
Time
Parallelism
Physically simultaneous processing.
Involves multiple PEs and/or independent device operations.
Code
D escriptor
Stack
Stack
D escriptor
D escriptor
D escriptor
T hread 1
T hread 2
Thread n
Stack
Agenda
Concurrency - definition and motivation
Thread safe programming: Safety and Liveliness
Safe concurrent design
SWING and Threads in NinJo
Garden
West
Turnstile
people
East
Turnstile
Ornamental garden
Uses Objects of classes Turnstile and Counter
Each Turnstile is a separate Thread
...
...
class
class Turnstile
Turnstile extends
extends Thread
Thread {{
NumberCanvas
NumberCanvas display;
display;
Counter
people;
Counter people;
Turnstile(NumberCanvas
Turnstile(NumberCanvas n,Counter
n,Counter c)
c)
{{ display
=
n;
people
=
c;
}
display = n; people = c; }
public
public void
void run()
run() {{
try{
try{
display.setvalue(0);
display.setvalue(0);
for
for (int
(int i=1;i<=Garden.MAX;i++){
i=1;i<=Garden.MAX;i++){
Thread.sleep(500);
Thread.sleep(500); //0.5
//0.5 second
second between
between
arrivals
arrivals
display.setvalue(i);
display.setvalue(i);
people.increment();
people.increment();
}}
}} catch
catch (InterruptedException
(InterruptedException e)
e) {}
{}
}}
}}
EGOWS 2005: Concurrency
}}
void
void increment()
increment() {{
int
int temp
temp == value;
value; //read
//read value
value
Simulate.HWinterrupt();
Simulate.HWinterrupt();
value=temp+1;
//write
value=temp+1;
//write value
value
display.setvalue(value);
display.setvalue(value);
}}
Why?
west
PC
shared code
east
PC
increment:
program
counter
read value
program
counter
write value + 1
}}
synchronized
synchronized void
void increment()
increment() {{
int
temp
=
value;
int temp = value; //read
//read value
value
Simulate.HWinterrupt();
Simulate.HWinterrupt();
value=temp+1;
//write
value=temp+1;
//write value
value
display.setvalue(value);
display.setvalue(value);
}}
Dining Philosophers
Five philosophers sit around a
circular table. Each philosopher
spends his life alternately thinking
and eating. In the centre of the table
is a large bowl of spaghetti. A
philosopher needs two forks to eat a
helping of spaghetti.
One fork is placed between each pair
of philosophers and they agree that
each will only use the fork to his
immediate right and left.
Dining Philosophers
Each FORK is a shared
resource with actions get
and put.
When hungry, each PHIL
must first get his right and
left forks before he can
start eating.
right
FORK
phil[0]:
PHIL
lef t
right
lef t
phil[4]:
PHIL
phil[1]:
PHIL
lef t
right
FORK
FORK
right
lef t
phil[2]:
PHIL
FORK
right
FORK
lef t
phil[3]:
PHIL
Trace to DEADLOCK:
phil.0.sitdown
phil.0.right.get
phil.1.sitdown
phil.1.right.get
phil.2.sitdown
phil.2.right.get
phil.3.sitdown
phil.3.right.get
phil.4.sitdown
phil.4.right.get
Deadlock-free Philosophers
Deadlock can be avoided by ensuring that a wait-for cycle
cannot exist. How?
Introduce an asymmetry into our definition of philosophers.
Use the identity I of a philosopher to make even numbered
philosophers get their left forks first, odd their right first.
Safety
typical failures:(race conditions)
read/write conflicts: 1 thread reads a value of a field, while another
writes it, the value, seen by the reading thread- hard to predict, depends
on, who won the race
write/write conflicts: 2 threads write to the same field, who will win the
race?
Possible results:
a graphical object is shown on a place , where it never was
a bank account is incorrect after the attempt to withdraw money in the
midst of an automatic transfer
Build a guaranteed safe system: to arrange that no objects ever
execute any methods, and thus conflicts can never encounter
Not a very productive form of programming
--> safety concerns must be balanced by liveliness concerns
Agenda
Concurrency - definition and motivation
Thread safe programming: Safety and Liveliness
Safe concurrent design
SWING and Threads in NinJo
Agenda
Concurrency - definition and motivation
Thread safe programming: Safety and Liveliness
Safe concurrent design
SWING and Threads in NinJo
If using Threads in SWING driven programs You will have to concern about all thread safety issues
in your design !
design rules
Thread control is done centrally (in Toplevel PAC agents)
No arbitrary thread creation allowed, jobs are queued for each
Scene(Map) separately, only 1 job is done for each scene at a time