Kĩ thuật lập trình - Lecture 19: Concurrent programming

Threads Exclusive Resource Access Exclusion Synchronization Cooperation between Threads Condition Synchronization Concurrent Programming Example

ppt16 trang | Chia sẻ: thuychi16 | Ngày: 24/01/2019 | Lượt xem: 377 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Kĩ thuật lập trình - Lecture 19: Concurrent programming, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Ivan MarsicRutgers UniversityLECTURE 19: Concurrent Programming1TopicsThreadsExclusive Resource AccessExclusion SynchronizationCooperation between ThreadsCondition SynchronizationConcurrent Programming Example2ParallelismWhat we want: Parallel instead of serial processing, to speed up serviceWhat problems to solve: Concurrent access to a resource (or software object) can lead to conflict  ambiguous result or “frozen” program3Concurrency: Exclusion Synchronization4Thread vs. ProcessProcess 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 processThreads run in the same programAn object from one thread can directly call a method on an object shared by another thread5Concurrent Programming -- ThreadsLifecycle of Java threads6Thread States: AliveAlive: 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 notifyAll() messageWaiting 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 availableWaiting for rendezvous: Invoking the method join(target) suspends the thread until the target thread returns from its run() methodSleeping: Invoking the method sleep(milliseconds) suspends the thread for the specified time7Exclusion Synchronization8Example: Bank Account Access by Two UsersConcurrent 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 placeThread 1 Thread 2oldBalance = account.getBalance(); ...newBalance = oldBalance + deposit; oldBalance = account.getBalance();account.setBalance(newBalance); newBalance = oldBalance - withdrawal;... account.setBalance(newBalance);9Exclusion Synchronization in Javapublic class SharedClass { ... public synchronized void method1( ... ) { ... }}acquirelockreleaselockshared objectpublic class AnyClass { ... public void method2( ... ) { ... synchronized (expression) { statement } ... }}acquirelockreleaselockshared objectSynchronized MethodsSynchronized Statements(a)(b)10Condition Synchronizationsuspend and wait resume the suspended work11Example: Safe Home AccessBackyard door:Access lockFront door:Access lockCentralComputer12Example of Concurrency GainsSingle thread – sequential service(a)Multiple threads – parallel service(b)13Where Latency Matters?While typing in the digits, the user does not notice latencyLatency becomes noticeable when waiting for validity check and device activationValidity computation is quick; device activation may be somewhat slowerThe longest latency is while the user is typing-in the keycode so we need to allow parallel entry of keycodes on both doorsBut ...all communication goes over the same serial cable:TenantRead Digits[ keycode valid ][else]Check ValidityActivate Devices[ keycode complete ][else]14Hardware ImplementationNOTE: Locks, lightbulb switch (with photosensor) and alarm bell are controlled by the central computer via the same serial port15Multithreaded ImplementationkeyFront : StringBufferkeyBack : StringBuffercontrlBack : ControllerThdcontrlFront : ControllerThd HomeAccessControlSystem_2x+ serialEvent(event : SerialPortEvent)Main Thread:interacts with serial I/O portHelper Thread:back door calculations and controlHelper Thread:front door calculations and controlShared ObjectShared Object( See Listing 5-6 in the book for details )16
Tài liệu liên quan