Docsity

Java Multithreading and Synchronization: Preventing Data Races and Deadlocks - Prof. Nelso, Study notes of Computer Science

Partial preview of the text

Download Java Multithreading and Synchronization: Preventing Data Races and Deadlocks - Prof. Nelso 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) {

for (int i = 0; i < 3; i++)

new DataRace().start(); 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 toeliminate 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 Thread will wait to acquire lock (stop execution)

If lock held by another thread

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 acquiringsame 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 singleatomic 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; } } }