



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
2
Creating Java threads Thread states Scheduling
Data races Locks Deadlock
3
Concurrent accesses to same shared variable, where at least one access is a write
Order of accesses may change result of program May cause intermittent errors, very hard to debug
public class DataRace extends Thread { static int x; // shared variable x causing data race public void run() { x = x + 1; } // access to x }
4
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 } }
5
6
Result depends on thread execution order!
7
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
8
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
9
Synchronized Objects in Java
Apply synchronized keyword to object Will acquire / release lock associated with object Mutual exclusion for code in synchronization block
Object x = new Object(); synchronized(x) { // acquire lock on x on entry ... // hold lock on x in block
block}^ // release lock on x on exit
10
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
11
Synchronized Methods In Java
12
Locks in Java
No other thread can get lock on x while in block Other threads can still access/modify x! Locked block of code ⇒ critical section
End of block reached Exit block due to return, continue, break Exception thrown
19
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 }
20
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; } } }
21
Issue 3) Avoiding Deadlock
Threads holding lock may be unable to obtain lock held by other thread, and vice versa Thread holding lock may be waiting for action performed by other thread waiting for lock Program is unable to continue execution (deadlock)
22
Deadlock Example 1
Object a; Object b; Thread1() { Thread2() { synchronized(a) { synchronized(b) { synchronized(b) { synchronized(a) { … … } } } } } }
// Thread1 holds lock for a, waits for b // Thread2 holds lock for b, waits for a
23
Deadlock Example 2
void swap(Object a, Object b) { Object local; synchronized(a) { synchronized(b) { local = a; a = b; b = local; } } }
Thread1() { swap(a, b); } // holds lock for a, waits for b Thread2() { swap(b, a); } // holds lock for b, waits for a
24
Abstract Data Type – Buffer
Transfers items from producers to consumers Very useful in multithreaded programs Synchronization needed to prevent multiple consumers removing same item
25
Buffer Implementation
Class BufferUser() { Buffer b = new Buffer();
ProducerThread() { // produces items Object x = new Object(); b.add(x); }
ConsumerThread() { // consumes items Object y; y = b.remove(); } }
26
Buffer Implementation
public class Buffer { private Object [ ] myObjects; private int numberObjects = 0; public synchronized add( Object x ) { myObjects[ numberObjects++ ] = x; } public synchronized Object remove() { while (numberObjects < 1) { ; // waits for more objects to be added } return myObjects[ --numberObjects ]; } } // if empty buffer, remove() holds lock and waits // prevents add() from working ⇒ deadlock
27
Eliminating Deadlock
public class Buffer { private Object [ ] myObjects; private int numberObjects = 0; public add( Object x ) { synchronized(this) { myObjects[ numberObjects++ ] = x; } } public Object remove() { while (true) { // waits for more objects to be added synchronize(this) { if (numberObjects > 0) { return myObjects[ --numberObjects ]; } } } } // if empty buffer, remove() gives up lock 28
Deadlock
In general, avoid holding lock for a long time Especially avoid trying to hold two locks May wait a long time trying to get 2nd^ lock
29
Synchronization Summary
To be discussed in future courses