

















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: Spring 2008;
Typology: Study notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Multithreading Overview
Creating Java threads Thread states Scheduling
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
Synchronization
Coordination of events with respect to time
May be needed in multithreaded programs to eliminate data races Incurs runtime overhead Excessive use can reduce performance
Lock
Entity can be held by only one thread at a time
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
Apply synchronized keyword to method Mutual exclusion for entire body of method Synchronizes on object invoking method
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
Mutual exclusion depends on threads acquiring same lock No synchronization if threads have different locks
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
Sequence of actions must be performed as single atomic transaction to avoid data race Ensure lock is held for duration of transaction
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; } } }