Lecture Slides for Synchronization in Java | CMSC 132, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-hny
koofers-user-hny 🇺🇸

5

(1)

10 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Synchronization in Java
CMSC 132
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Lecture Slides for Synchronization in Java | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

Synchronization in Java

CMSC 132

Department of Computer Science

University of Maryland, College Park

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?

Compiler can reorder statements

Or keep values in registers

Processor can reorder them On multi-processor, values not synchronized toglobal memory The memory model is designed to allowaggressive optimization

including optimizations no one has implemented yet

Good for performance

bad for your intuition about insufficientlysynchronized code

Synchronization

Uses

Marks when a block of code must not be interleavedwith code executed by another thread Marks when information can/must flow betweenthreads

Notes

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

All Java objects provide locks

Apply

synchronized

keyword to object

Mutual exclusion for code in synchronization

block

Example

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

Java methods also provide locks

Apply

synchronized

keyword to method

Mutual exclusion for entire body of method Synchronizes on object invoking method

Example

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

What would happen if the lock field were notstatic? Why don’t we just make the run methodsynchronized? Why don’t we just synchronize on x?

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

Issue 1: Use same lock to provide mutualexclusion Issue 2: Ensure atomic transactions Issue 3: Avoiding deadlock Issue 4: Using wait & notify Let’s see those issues in detail

Locks in Java

Single lock for all threads (mutual exclusion) Separate locks for each thread (no synchronization)

Potential problem

Sequence of actions must be performed as single atomic transaction

to avoid data race

Ensure lock is held for duration of transaction

Example

synchronized(

lock

int tmp = x;

// both statements must// be executed together

x = tmp;

// by single thread

Issue 2: Atomic Transactions