concurrency
play

Concurrency November 27, 2007 1 Thread Classes - PowerPoint PPT Presentation

Concurrency November 27, 2007 1 Thread Classes <<interface>> Runnable void run() 0..1 target Thread void interrupt() void sleep (long millis) void start() 2 Wednesday, November 28, 2007 (Simplified) Thread State Machine


  1. Concurrency November 27, 2007 1 Thread Classes <<interface>> Runnable void run() 0..1 target Thread void interrupt() void sleep (long millis) void start() 2 Wednesday, November 28, 2007

  2. (Simplified) Thread State Machine Terminated run method sleep Sleeping ends time out, New Runnable interrupt start 3 HelloWithCancel Classes <<interface>> SlowCounter Runnable void run() void run() 0..1 target Thread Hello void interrupt() void actionPerformed void sleep (long millis) void start() 4 Wednesday, November 28, 2007

  3. :Hello new :SlowCounter Creating and Starting new (SlowCounter) the thread :Thread start() run 2nd thread running sleep () interrupt() Interrupting the throw InterruptedException 2nd thread 5 Shared Data An object can be referenced by more than one thread -> shared data Changing the value in one thread can affect the behavior of the other thread! BankAccount Consider a joint int balance account where each void withdraw (int amount) account holder void deposit (int amount) simultaneously uses int getBalance() a different ATM 6 Wednesday, November 28, 2007

  4. Race Condition Account Withdrawer Depositer balance = 1000 withdraw(10) newBalance = 990 balance = 990 Thread Color deposit(10) Key newBalance = 1000 Main Withdrawer balance = 1000 Depositer withdraw(10) in withdraw newBalance = 990 deposit(10) in deposit newBalance = 1010 balance = 1010 balance = 990 7 Locking Objects Need to assure that deposit and withdraw do not interleave Java solution: Declare methods to be synchronized Entering a synchronized method locks the object Exiting unlocks it If object is already locked, Java waits at the beginning of the synchronized method 8 Wednesday, November 28, 2007

  5. Synchronized Methods public synchronized void withdraw (int amount) { //balance = balance - amount; int newBalance = balance - amount; Thread.yield(); balance = newBalance; notifyListeners(); } public synchronized void deposit (int amount) { //balance = balance + amount; int newBalance = balance + amount; Thread.yield(); balance = newBalance; notifyListeners(); } 9 Threads Terminated run method sleep Sleeping ends time out, start New Runnable interrupt waiting lock acquired yield for a lock Blocked 10 Wednesday, November 28, 2007

  6. Account Withdrawer Depositer balance = 1000 withdraw(10) lock newBalance = 990 balance = 990 unlock deposit(10) lock newBalance = 1000 balance = 1000 unlock withdraw(10) lock in withdraw newBalance = 990 deposit(10) wait for lock balance = 990 unlock lock in deposit newBalance = 1000 balance = 1000 11 unlock Wait/Notify What if we don’t want to allow a withdrawal if it would bring the balance below $0? Suppose we know a deposit will happen soon? � public synchronized void withdraw (int amount) { � � / /balance = balance - amount; if (balance < 10) { ??? throw an exception ??? } � � int newBalance = balance - amount; � � balance = newBalance; � � notifyListeners(); � } 12 Wednesday, November 28, 2007

  7. Wait/Notify wait() temporarily gives up the lock When should we get it back? � public synchronized void withdraw (int amount) { � � / /balance = balance - amount; while (balance < 10) { wait(); } � � int newBalance = balance - amount; � � balance = newBalance; � � notifyListeners(); � } 13 Wait/Notify notify() puts waiting threads in queue for the lock public synchronized void deposit (int amount) { //balance = balance + amount; int newBalance = balance + amount; balance = newBalance; notifyAll(); notifyListeners(); } 14 Wednesday, November 28, 2007

  8. Threads Terminated run method sleep Sleeping ends time out, start New Runnable interrupt waiting lock acquired wait for a lock Waiting Blocked notify 15 Account Withdrawer Depositer balance = 90 withdraw(100) lock deposit(100) wait for lock newBalance = -10 unlock and wait lock balance = 190 notifyAll unlock lock in withdraw newBalance = 90 balance = 90 unlock 16 Wednesday, November 28, 2007

  9. Thread Safety A class is thread safe if it is correct even if accessed by multiple threads 2 simple cases: A class with no instance or static variables An immutable class Otherwise: Find all methods that read and write the same instance variable Only need to worry about non-final instance variables Make those methods synchronized This will avoid race conditions but might introduce...deadlock! Aye, matie! 17 Wednesday, November 28, 2007

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend