28
Synchronization in Concurrent Programming Amit Gupta

Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Synchronization in Concurrent Programming

Amit Gupta

Page 2: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Announcements

• Project 1 grades are out on blackboard.

• Detailed Grade sheets to be distributed after class.

• Project 2 grades should be out by next Thursday.

Page 3: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Introduction

• Concurrent programs allow multiple threads to be scheduled and executed with no user control.

• JVM and underlying OS responsible for scheduling threads onto processor cores.

• Program should work correctly despite the fact that different execution of same program lead to different sequences of thread execution.

Page 4: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Bank Account: Problem

• Classic Two people: One Account problem.

• Ryan and Monica share a common bank account.

• Both of them agree not to overdraw the account.

• So, the procedure is, whoever wants to withdraw money must check the balance in account before making withdrawal.

• Seems to be good solution with no problems.

Page 5: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Bank Account: Problem

• Ryan needs 50$, checks the account and saw it as 100$ and plans to withdraw money.

• But first he falls sleep.

• Monica comes up, checks the account and withdraws 100$ as Ryan has not made his withdrawal yet.

• Ryan wakes up and makes his withdrawal without checking account balance and we are overdrawn.

Page 6: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Bank Account: Problem

• Is there a solution??

• We cannot stop Ryan from sleeping but can we make sure that Monica can’t get her hands on bank account until Ryan wakes up?

Page 7: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Bank Account Problem in Code

• Bank Account class 

• Ryan and Monica check balance before making any withdrawal.

• RyanAndMonicaJob class represents the behavior that Ryan and Monica both have – checking the balance and making withdrawals.

Page 8: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Bank Account Problem in Code

Page 9: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Observations

• Both threads share same instance of Bank Account.

• In run() method, the thread loops through and tries to make a withdrawal with each iteration.

• makeWithdrawal() method checks the account balance and if there is enough money, go to sleep, then wake up and complete the withdrawal.

Page 10: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Problem

• Ryan checks balance, sees enough money and falls asleep.

• Meanwhile, Monica comes in and checks the balance, sees enough money and falls asleep.

• Ryan wakes up and completes withdrawal.

• Monica wakes up and completes withdrawal.(PROBLEM)

• Monica’s check of the account was not valid because Ryan was in the middle of making a withdrawal.

Page 11: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Lock for Account Access

Page 12: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Lock for Account Access

• We need the makeWithDrawal() method to run as one atomic thing.

• In other words, we need to make sure once a thread has checked the account balance, that thread has a guarantee that it can wake up and finish the withdrawal before any other thread can check the account balance.

Page 13: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

synchronized keyword

• synchronized keyword means that a thread needs a key in order to access the synchronized code.

• To protect data, synchronize the methods that act on data i.e we lock the method that does banking transaction.

Page 14: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Lost Update Problem

• Another classic concurrency problem.

• Two threads trying to increment balance.

• We increment the balance by 1 to whatever the value of balance was at the time we read it not the current value.

• Incrementing balance not an atomic operation.

Page 15: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Solution

• Make the increment() method atomic

• Once the thread enters the method, we have to make sure that all the steps in method complete before any other thread can enter the method.

Page 16: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Locks in Java

• Every object in Java has a lock and the lock has only one key.

• A thread can enter a synchronized method only if the thread can get the key to the object’s lock.

• Locks are per object not per method. So, while the thread is holding the key, no other threads can enter any of that object’s synchronized methods because the key has already been grabbed by the other thread.

Page 17: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

synchronized on a code block

• Synchronized applied to a block of code gives more flexibility.

• First, we can control how much code needs to be synchronized.

• We can choose which object we want to synchronize on.

• public void danger(){synchronized(this){

message = “Danger” + message }

}

• Private static Object lock1 = new Object();Private static Object lock1 = new Object();

synchronized(lock1){// do dangerous thing 1

}

synchronized(lock2){// do dangerous thing 1

}

Page 18: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Why don’t we synchronize every method??

• A synchronized method has a certain amount of overhead.

• It restricts concurrency and make other threads to get in line and wait.

• It can lead to DEADLOCKS.

• Synchronize only the bare minimum that needs to be synchronized.

Page 19: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Busy Waiting

• Many times a thread waits for another thread to finish some task so that it can process the results.

• While(true){

synchronized(this){

if(votingComplete)

break;

}

}

countVotes();

Page 20: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Busy Waiting

• On a uniprocessor machine, the counting thread is running through a while loop and slowing down the job of collecting thread.

• On a multiprocessor machine, the counting thread is still wasting CPU cycles that other thread could use.

• This is called as BUSY WAITING.

Page 21: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

wait() and notify()

• When a thread calls wait(), it gives up the corresponding lock it is holding and runs again.

• Many other threads may be waiting on this particular lock.

• In order to notify a waiting thread, the other thread calls the notify() method.

• wait() and notify() must be called within a synchronized code.

• synchronized(this){while(!votingComplete)

wait();}countVotes();

• synchronized(this){votingComplete = true;

notifyAll();}

Page 22: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

notify() and notifyAll()

• notify() wakes up one thread waiting on the lock object 

• notifyAll() can be called to wake them all up.

Page 23: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Producer/Consumer Problem

• Classic Example in Concurrent programming.

• One thread produces some data and other thread consumes that data.

• Producer/Consumer problem use a boundedbuffer which store items from the producer until the consumer can take them away.

Page 24: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Producer/Consumer Problem• public class Buffer{

public final static int SIZE = 10;private Object[] objects = new Object[SIZE];private int count = 0;

public synchronized void addItem(Object object) throws InterruptedException{while(count == SIZE)

wait();object[count++] = object;notifyAll();

}

public synchronized void removeItem() throws InterruptedException{while(count == 0)

wait();object = object[count‐‐];notifyAll();return object;

}}

Page 25: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Deadlock

• Deadlock happens when you have two threads, both of which holding the key of lock the other thread wants.

• Java has no mechanism to handle deadlock. So, we should write design our multithreaded code carefully.

Page 26: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

A Simple Deadlock Scenario

Page 27: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Other Synchronization Challenges

• Starvation and LiveLock

• Sequential Execution

• Priority Inversion

Page 28: Synchronization in Concurrent Programmingcs180/Spring2010Web/...Announcements • Project 1 grades are out on blackboard. • Detailed Grade sheets to be distributed after class. •

Questions