1 / 14

Ivan Marsic Rutgers University

LECTURE 19: Concurrent Programming. Ivan Marsic Rutgers University. Topics. Threads Exclusive Resource Access Exclusion Synchronization Cooperation between Threads Condition Synchronization Concurrent Programming Example. Thread vs. Process.

gilles
Download Presentation

Ivan Marsic Rutgers University

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LECTURE 19: Concurrent Programming Ivan Marsic Rutgers University

  2. Topics • Threads • Exclusive Resource Access • Exclusion Synchronization • Cooperation between Threads • Condition Synchronization • Concurrent Programming Example

  3. Thread vs. Process • Process roughly corresponds to a program (*program can “spawn” many processes) • Processes communicate using inter-process 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

  4. Concurrent Programming -- Threads Lifecycle of Java threads

  5. 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 (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 notifyAll() 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

  6. Concurrency: Exclusion Synchronization

  7. Exclusion Synchronization

  8. Exclusion Synchronization in Java shared object public class AnyClass { ... public void method2( ... ) { ... synchronized (expression) { statement } ... } } public class SharedClass { ... public synchronized void method1( ... ) { ... } } acquire lock acquire lock release lock release lock shared object (a) (b) Synchronized Methods Synchronized Statements

  9. Condition Synchronization

  10. Example: Safe Home Access Central Computer Backyard door: Access lock Front door: Access lock

  11. Example of Concurrency Gains Single thread – sequential service (a) Multiple threads – parallel service (b)

  12. [ keycode complete ] [ keycode valid ] Activate Devices Read Digits Check Validity Tenant [else] [else] Where Latency Matters? ● 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 the keycode so we need to allow parallel entry of keycodes on both doors ● But ... all communication goes over the same serial cable:

  13. Hardware Implementation NOTE: Locks, lightbulb switch (and photosensor) and alarm bell controlled by the central computer via the same serial port

  14. Main Thread: interacts with serial I/O port Helper Thread: front door calculations and control Helper Thread: back door calculations and control HomeAccessControlSystem_2x + serialEvent(event : SerialPortEvent) contrlFront : ControllerThd contrlBack : ControllerThd keyFront : StringBuffer keyBack : StringBuffer Shared Object Shared Object Multithreaded Implementation ( See Listing 5-6 in the book for details )

More Related