CSE 132 Question The following four numbers are in 8bit 2s complement form 00000000 10101010 01010101 11111111 Which order below represents lowest to highest A 00000000 01010101 10101010 11111111 ID: 560427
Download Presentation The PPT/PDF document "Concurrency (2)" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Concurrency (2)
CSE 132Slide2
Question
The following four numbers are in 8-bit 2’s complement form:
00000000 10101010 01010101 11111111
Which order below represents lowest to highest?
A: 00000000 01010101 10101010 11111111
B: 10101010 11111111 00000000 01010101
C: 11111111 10101010 00000000 01010101
D: 11111111 00000000 01010101 10101010
E: none of the aboveSlide3
Synchronization
Implicit “lock” associated with every object
Synchronized methods
acquire object’s lock prior to entering method and then release lock once method is complete
Synchronized
statement allows finer control (illustrated next)
Either way, lock enforces
mutual exclusion
, only one thread at a time is allowed in “critical section,” or region of code controlled by lockSlide4
Synchronized statement
public void
addName
(String name) {
synchronized
(this) {
lastName
= name;
nameCount
++;
}
nameList.add
(name);
}
Argument is object whose lock is acquired
Doesn’t synchronize add() method invocationSlide5
PropertyChangeSupport
Bound properties – anonymous notification when a property’s value changes
Properties are identified by strings
Must be identical for both publishers and subscribers
Publishers indicate to PCS that property changed
Subscribers are then notified of new property value
Provides excellent
observability
for multi-threaded applicationsSlide6
Example
Publishers – three
RandomPerson
objects
Subscribers – interested in different properties about the publishers
Properties – things persons are doing:
Walk “strolling”
TV “watching TV”
Nap “snoozing”
Guitar “playing guitar”Slide7
Dining Philosophers
Five silent philosophers sit at a round table with bowls of rice. A chopstick is placed between each pair of adjacent philosophers.
Each philosopher must alternately think and eat. Eating is not limited by the amount of rice left: assume an infinite supply. However, a philosopher can only eat while holding both the chopstick to the left and the chopstick to the right.
Each philosopher can pick up an adjacent chopstick, when available, and put it down, when holding it. These are separate actions: chopsticks must be picked up and put down one by one.
How do we design a concurrent algorithm such that each philosopher won't starve, i.e., can forever continue to alternate between eating and thinking.Slide8