






























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
Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 38
This page cannot be seen from the preview
Don't miss anything!































Synchronization Overview
Data races Locks Deadlock Wait / Notify
Data Race
Definition
Concurrent accesses to same sharedvariable, where at least one access is awrite Can expose all sorts of really weird stuffthe compiler and processor are doing toimprove performance See example Race1.java
Quiz Time
x = y = 0
x = 1 j = y
Thread 1
y = 1 i = x
Thread 2
Can this result in i = 0 and j = 0?
start threads
How Can This Happen?
Or keep values in registers
including optimizations no one has implemented yet
bad for your intuition about insufficientlysynchronized code
Synchronization
Marks when a block of code must not be interleavedwith code executed by another thread Marks when information can/must flow betweenthreads
Incurs a small amount of runtime overhead
if only used where you might need tocommunicate between threads, not significant used everywhere, can add up
Synchronized Objects in Java
Apply
synchronized
keyword to object
Mutual exclusion for code in synchronization
block
Object x = new Object();void foo() {synchronized(x) {
// acquire lock on x on entry
// hold lock on x in block
// release lock on x on exit
block
Synchronized Methods In Java
Apply
synchronized
keyword to method
Mutual exclusion for entire body of method Synchronizes on object invoking method
synchronized void foo() { …code… }
// shorthand notation for
void foo() {
synchronized (this) { …code… } }
block
Using Synchronization
public class UseSynchronization implements Runnable {
static int
x
static Object lock = new Object();public void run() {
synchronized(lock) {
int tmp =
x
x
= tmp+1; }
} }
Questions
Locks in Java
Properties
No other thread can get lock on x while in block Does not protect fields of x
except by convention other threads can access/update fields but can’t obtain lock on x By convention, lock x to obtain exclusive access to x Locked block of code
⇒⇒⇒⇒
critical section
Lock is released when block terminates
No matter how the block terminates:
End of block reached Exit block due to return, continue, break Exception thrown
Synchronization Issues
Locks in Java
Single lock for all threads (mutual exclusion) Separate locks for each thread (no synchronization)
Sequence of actions must be performed as single atomic transaction
to avoid data race
Ensure lock is held for duration of transaction
synchronized(
lock
int tmp = x;
// both statements must// be executed together
x = tmp;
// by single thread
Issue 2: Atomic Transactions