1Lecture 20Managing threadsSynchronizationMotivation• Organizing an expedition – people that are assigned different tasks need to communicate and synchronize their actions• Threads in the previous lecture – they run independently and have concurrent access to the output stream ⇒ the results are muddled• Reading, processing and writing data in separate threads – synchronization is necessaryRead data1Process data1Write data1 Write data2 Write data3Process data3Process data2Read data2 Read data3 Read data42The need for synchronization• Ex: 2 people have a joint account; say they access it simultaneously: one pays a check in, the other gets cash from an ATMtime timeAccountBank clerk’s desktopATM$500Get balance: $500Get balance: $500Pay check in:$500 + $200 = $700Get cash:$500 - $100 = $400$400$700Set balance: $700Set balance: $400Synchronization• When two or more threads share access to the same objects, a race condition occurs• In order to avoid data corruption, access to the objects has to be synchronized: only one thread should be able to access an object• If a thread accesses an object, the object needs to be locked; it is unlocked when the thread finishes accessing the object• If a thread attempts to access a locked object, it is put on hold: it has to wait until the object is free3Method synchronization•Ex:public synchronized void transfer(int from, int to,int amount) {…}– When a thread calls a synchronized method, it is guaranteed that the method will finish before another thread can execute any synchronized method on the same object– Analogy: rest room, telephone booth– Other threads are still free to call unsynchronized methods on alocked object; Ex: int size(); // if the size never changes– It is the object, not the method that is locked; the same synchronized method can be called on multiple objects at the same time– A synchronized method may call a synchronized method on another object and lock that object as well– If one thread calls a synchronized static method of a class, allthe other static methods are blockedStatement block synchronizationsynchronized(anObject) {// statements accessing the object}– Synchronization can be applied on any object– When a block that is synchronized on an object is executing, no other code clock or method that is synchronized on the same object can execute• Consequence: do not include more statements than necessary in a synchronized blocksynchronized(obj) {//Read parameters from a file – this should be before the block//Set some fields of obj based on the parameters}4wait() and notify()/notifyAll()• In multithreaded programming, a thread may need to wait until another thread has completed some taskpublic synchronized void write() {while(/*data is not available*/) {wait();}// write the data}• When some task was finished, a notification needs to be sent around to the potential “beneficiaries”public synchronized void process() {// produce some data// notifyAll(); // or notify();}A Bank application• Start with a poor implementation and improve it– Bank – can perform transactions– Transaction – defines the amount and if it’s credit/debit– Account – keeps track of the balance– Clerk – simulation of the client application running on a bank clerk’s desktop• Problems– Bank’s transactions not synchronized → SynchronizedBank– Only one account → Create more in MultipleAccounts– Sync on bank means only one account can be accessed at a time → sync accounts in SynchronizedBlocks– Single transaction in in-tray → use synchronized list of required transaction in
View Full Document