LECTURE 19 Concurrent Programming Ivan Marsic Rutgers University
- Slides: 16
LECTURE 19: Concurrent Programming Ivan Marsic Rutgers University 1
Topics § Threads § Exclusive Resource Access – Exclusion Synchronization § Cooperation between Threads – Condition Synchronization § Concurrent Programming Example 2
Parallelism § What we want: Parallel instead of serial processing, to speed up service § What problems to solve: Concurrent access to a resource (or software object) can lead to conflict ambiguous result or “frozen” program 3
Concurrency: Exclusion Synchronization 4
Thread vs. Process § Process roughly corresponds to a program (*program can “spawn” many processes) § Processes communicate using interprocess communication (pipes, sockets, shared memory, …) – An object from one process cannot directly call a method on an object shared by another process § Threads run in the same program – An object from one thread can directly call a method on an object shared by another thread 5
Concurrent Programming -- Threads Lifecycle of Java threads 6
Thread States: Alive § Alive: After a thread is start()-ed, it becomes alive: – Runnable: The thread can be run when the OS scheduler can arrange it (and nothing prevents it from being run) – Blocked: The thread could be run, but there is something that prevents it (e. g. , another thread is holding the resource needed for this thread to do its work). While a thread is in the blocked state, the scheduler will simply skip over it and not give it any CPU time. A thread can become blocked for the following reasons: • Waiting for notification: Invoking the method wait() suspends the thread until the thread gets the notify() or notify. All() message • Waiting for I/O or lock: The thread is waiting for an input or output operation to complete, or it is trying to call a synchronized method on a shared object, and that object’s lock is not available • Waiting for rendezvous: Invoking the method join(target) suspends the thread until the target thread returns from its run() method • Sleeping: Invoking the method sleep(milliseconds) suspends the thread for the specified time 7
Exclusion Synchronization 8
Example: Bank Account Access by Two Users § Concurrent read/write of the same data by several § threads “race condition” or “race hazard” The outcome of the execution depends on the particular order in which the access takes place Thread 1 Thread 2 old. Balance = . . . account. get. Balance(); new. Balance = old. Balance = old. Balance + deposit; account. get. Balance(); account. set. Balance(new. Balance ); new. Balance = old. Balance - withdrawal; . . . account. set. Balance(new. Balance ); 9
Exclusion Synchronization in Java shared object acquire lock release lock public class Shared. Class { . . . public synchronized void method 1(. . . ) { . . . } } acquire lock release lock public class Any. Class { . . . public void method 2(. . . ) { . . . synchronized (expression) { statement } . . . } shared object } (a) (b) Synchronized Methods Synchronized Statements 10
Condition Synchronization suspend and wait … … resume the suspended work 11
Example: Safe Home Access Central Computer Backyard door: Access lock Front door: Access lock 12
Example of Concurrency Gains Single thread – sequential service (a) Multiple threads – parallel service (b) 13
Where Latency Matters? [ keycode complete ] Read Digits Tenant [else] [ keycode valid ] Check Validity Activate Devices [else] § While typing in the digits, the user does notice § § latency Latency becomes noticeable when waiting for validity check and device activation Validity computation is quick; device activation may be somewhat slower The longest latency is while the user is typing-in the keycode so we need to allow parallel entry of keycodes on both doors But. . . all communication goes over the same serial cable: 14
Hardware Implementation NOTE: Locks, lightbulb switch (with photosensor) and alarm bell are controlled by the central computer via the same serial port 15
Multithreaded Implementation Main Thread: interacts with serial I/O port Helper Thread: front door calculations and control Helper Thread: back door calculations and control Home. Access. Control. System_2 x + serial. Event(event : Serial. Port. Event) contrl. Front : Controller. Thd contrl. Back : Controller. Thd key. Front : String. Buffer key. Back : String. Buffer Shared Object ( See Listing 5 -6 in the book for details ) 16
- Ivan marsic rutgers
- Ivan marsic
- Ivan milat deus ex machina
- Ivan marsic
- Martina maršić
- Synchronization algorithms and concurrent programming
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- York u myfile
- C data types with examples
- Perbedaan linear programming dan integer programming
- Greedy programming vs dynamic programming
- What is in system programming
- Integer programming vs linear programming
- Perbedaan linear programming dan integer programming
- What are delegated reserved and concurrent powers
- Concurrent validity nedir
- Concurrent in os