Java Synchronization for Object Oriented Programming II | 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: Spring 2008;

Typology: Study notes

Pre 2010

Uploaded on 07/29/2009

koofers-user-0o2-1
koofers-user-0o2-1 🇺🇸

8 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Synchronization in Java
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

Partial preview of the text

Download Java Synchronization for Object Oriented Programming II | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Synchronization in Java

Department of Computer Science

University of Maryland, College Park

Multithreading Overview

Motivation & background

Threads

Creating Java threads Thread states Scheduling

Synchronization

Data races Locks Deadlock

Data Race Example public class DataRace extends Thread { static int common = 0; public void run() { int local = common; // data race local = local + 1; common = local; // data race } public static void main(String[] args) throws InterruptedException { int max = 3; DataRace[] allThreads = new DataRace[max]; for (int i = 0; i < allThreads.length; i++) allThreads[i] = new DataRace(); for (DataRace t : allThreads) t.start(); for (DataRace t : allThreads) t.join(); System. out.println(common); // may not be 3 } }

Data Race Example

Sequential execution output

Synchronization

Definition

Coordination of events with respect to time

Properties

May be needed in multithreaded programs to eliminate data races Incurs runtime overhead Excessive use can reduce performance

Lock

Definition

Entity can be held by only one thread at a time

Properties

A type of synchronization Used to enforce mutual exclusion Thread can acquire / release locks Only 1 thread can acquire lock at a time Thread will wait to acquire lock (stop execution) If lock held by another thread Used to implement monitors Only 1 thread can execute (locked) code at a time

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 foo() { …code… } // shorthand notation for foo() { synchronized (this) { …code… } } block

Synchronized Methods In Java

Synchronization Example

Lock Example public class DataRace extends Thread { static int common = 0; static Object o; // all threads use o’s lock public void run() { synchronized(o) { // single thread at once int local = common; // data race eliminated local = local + 1; common = local; } } public static void main(String[] args) { o = new Object(); } }

Issue 1) Using Same Lock

Potential problem

Mutual exclusion depends on threads acquiring same lock No synchronization if threads have different locks

Example

foo() { Object o = new Object(); // different o per thread synchronized(o) { // potential data race } }

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

Issue 2) Atomic Transactions

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(o) { int local = common; // all 3 statements must local = local + 1; // be executed together common = local; // by single thread }

Lock Example Incorrect Version public class DataRace extends Thread { static int common = 0; static Object o; // all threads use o’s lock public void run() { int local; synchronized(o) { local = common; } // transaction not atomic synchronized(o) { // data race may occur local = local + 1; // even using locks common = local; } } }