Java Concurrency

From Suhrid.net Wiki
Jump to navigationJump to search

Concurrent Programming

  • The name given to programming notations and techniques to express potential parallelism and solve the resulting communication and synchronization problems.
  • Why do we need it ? To allow the expression of potential parallelism so that more than CPU can solve the problem.
  • To model the parallelism in the real world.
  • Alternative is difficult: programmer must construct sequential program as a cyclic execution of task to handle various concurrent activities.
  • This make programming difficult and introduction of structures which are irrelevant to the problem at hand.
  • Parallel execution on multiple CPU's is difficult.
  • A concurrent program is a collection of autonomous sequential processes executing logically in parallel.
  • Each process has a single thread of control.
  • The actual implementation of these collection of processes can be via:
    • Multiprogramming: Multiple processes multiplex their executions on a single processor.
    • Multiprocessing: Multiple processes multiplex their executions on a a multiprocessor system where there is access to shared memory.
    • Distributed processing: Multiple processes multiplex their executions on several processors where they do not share memory.

Problems with concurrent programming

  • Deadlock is a condition where each concurrent activity is waiting for another to perform an operation.
  • Starvation is a condition where one or more concurrent activities are continuously denied resources as a result of others.
  • Interference may occur when more than one concurrent activity attempt to update shared data.
  • Livelock A livelock is similar to a deadlock, except that the states of the processes involved in the livelock constantly change with regard to one another, none progressing.
    • A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time.

Real time systems

  • A real time system is any system which has to respond to a stimuli within a finite and specified period.
  • The correctness depends on both the result and the time it was delivered.
  • Failure to respond is as bad as the wrong response.
  • Types of real time systems
    • Hard real-time — systems where it is absolutely imperative that responses occur within the required deadline. E.g. Flight control systems.
    • Soft real-time — systems where deadlines are important but which will still function correctly if deadlines are occasionally missed. E.g. Data acquisition system.
    • Firm real-time — systems which are soft real-time but in which there is no benefit from late delivery of service.

Characteristics of RTS

  • Concurrent control of separate system components.
  • Facilities to interact with special purpose hardware.
  • Extreme reliability and safe.
  • Guaranteed response times.

Java Concurrency

  • Java supports threads.
  • In the native model, each Java thread maps to a single OS thread.
  • In the green thread model, threads are invisible to the OS. The thread library handles all the threading.
  • There are various models in which concurrency can be integrated with OOP:
    • Asynchronous method calls
    • Early returns from methods.
    • Futures
    • Active Objects
  • Java adopts the "active object" model for concurrency.
  • What are active objects ? From [1]
  • Active objects are different from passive objects (normal objects) because passive objects re-use the execution context of other objects who call methods on them. To make the distinction clearer, consider a normal Java object. When a second object calls a method on it, that method executes inside the same JVM process and thread as the caller method. Many times the object being called will have some concurrency requirements - access to that object may not be thread-safe so it must be serialized, or perhaps internally the called object serializes access to some data. On the other hand, when a method is called on an active object, control returns immediately to the caller, and the active object executes the method in some execution context that it manages. The active object may keep an internal worker thread (or pool of threads) to perform execution. A more advanced active object may delegate execution to a different process or a different computer.

Communication and Synchronization

  • Approaches are broadly classified as shared variable or message passing.
  • A monitor is a popular concurrency model.
  • A monitor is essentially an object where each of its operation executes in mutual exclusion.
  • Monitors provide condition variables with two operations that can be called when the lock is held.