Lecture 19: Concurrent programming. The main contents of this chapter include all of the following: Threads, exclusive resource access, cooperation between threads, concurrent programming example. | Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming Topics Threads Exclusive Resource Access Exclusion Synchronization Cooperation between Threads Condition Synchronization Concurrent Programming Example 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 Concurrency: Exclusion Synchronization 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 Concurrent Programming -- Threads . | Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming Topics Threads Exclusive Resource Access Exclusion Synchronization Cooperation between Threads Condition Synchronization Concurrent Programming Example 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 Concurrency: Exclusion Synchronization 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 Concurrent Programming -- Threads Lifecycle of Java threads 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 (., 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 .