
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Major points from this lecture are: Race Conditions, Synchronization, Java Synchronization, Locking Works, Locks Are Associated With Objects, Visualizing Deadlock, Deadlocks Always Involve Cycles, Dealing With Deadlocks, Producer, Consumer . Object-Oriented Programming and Data Structures course includes program structure and organization, object oriented programming, graphical user interfaces, algorithm analysis, recursion, data structures (lists, trees, stacks, queues, heaps, search trees, has
Typology: Lecture notes
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























public^ void^ doSomething()
synchronized^ (this)
public^ synchronized^ void^ doSomething() ...} }
this is equivalent to
X^
Y
-^ They^ can^ include
2 or^ more^ threads
or processes^ in
a^ waiting^ cycle
-^ Other^ properties:^ ā^ The^ locks
need^ to^ be^ mutually
exclusive^ (no sharing^ of^ the
objects^ being
locked)
-^ The^ application
wonāt^ give^ up
and^ go^ away^ (no
timer^ associated
with^ the^ lock^
request)
-^ There^ are^ no
mechanisms^
for^ one^ thread
to^ take
locked^ resources
away^ from^ another thread^ ā^ no^ āpreemptionā
ā... drop that mouse oryouāll be down to 8 livesā
class^ BoundedBuffer
int^ putPtr^ =^ 0,^ getPtr^ =^ 0;^ //^ Next^ slot^ to^ use int^ available^ =^
Items^ currently^ available final^ int^ K^ =^ 10;
//^ buffer
capacity T[]^ buffer^ =^ new
public^ synchronized
void^ produce(T^ item)^ { while(available^ ==^ K)^ this.wait();
//^ Wait^ until^ not^ full
buffer[putPtr++^ %^ K]^ =^ item; ++available;this.notifyall();
//^ Signal:^ not^ empty } public^ synchronized
T^ consume()^ { while(available^ ==^ 0)^ this.wait();
//^ Wait^ until^ not
empty
--available;T^ item^ =^ buffer[getPtr++
this.notifyall();
//^ Signal:^ not^ full return^ item;} }
class BST {Object name;^
// Name of this nodeObject value; // Value of associated with that nameBST left, right; // Children of this node// Constructorpublic void BST(Object who, Object what) { name = who; value = what; }// Returns value if found, else nullpublic synchronized Object get(Object goal) {if(name.equals(goal)) return value;if(name.compareTo(goal) < 0) return left==null? null: left.get(goal);return right==null? null: right.get(goal);} // Updates value if name is already in the tree,
else adds new BST node public synchronized void put(Object goal, object value) {if(name.equals(goal)) { this.value = value; return; }if(name.compareTo(goal) < 0) {if(left == null) { left = new BST(goal, value); return; }left.put(goal, value);} else {if(right == null) { right =
new BST(goal, value); return; } right.put(goal, value);} } }