Posts

Showing posts with the label Concurrency patterns

Half-Sync/Half-Async Pattern

Image
Intent The Half-Sync/Half-Async pattern decouples synchronous I/O from asynchronous I/O in a system to simplify concurrent programming effort without degrading execution efficiency. Decompose the services of concurrent software into two separated layers�synchronous and asynchronous�and add a queueing layer to mediate communication between them. Process higher-level services, such as domain functionality, database queries, or file transfers, synchronously in separate threads or processes. Conversely, process lower-level system services, such as short-lived protocol handlers driven by interrupts from network hardware, asynchronously. If services in the synchronous layer must communicate with services in the asynchronous layer, have them exchange messages via a queuing layer. Note : Concurrent software often performs both asynchronous and synchronous service processing. Asynchrony is used to process low-level system services efficiently, synchrony to simplify application service process...

Guarded Suspension Pattern

Image
Intent Use Guarded suspension pattern to handle a situation when you want to execute a method on object which is not in a proper state. Wikipedia Says In concurrent programming, guarded suspension is a software design pattern for managing operations that require both a lock to be acquired and a precondition to be satisfied before the operation can be executed. The guarded suspension pattern is typically applied to method calls in object-oriented programs, and involves suspending the method call, and the calling thread, until the precondition (acting as a guard) is satisfied. Source code Implementation is based on GuardedQueue , which has two methods: get and put, the condition is that we cannot get from empty queue so when thread attempt to break the condition we invoke Object's wait method on him and when other thread put an element to the queue he notify the waiting one that now he can get from queue. Class Diagram Class diagram of Guarded Suspension Pattern Step 1:  Create...

Reader Writer Lock

Image
Intent Suppose we have a shared memory area with the basic constraints detailed above. It is possible to protect the shared data behind a mutual exclusion mutex, in which case no two threads can access the data at the same time. However, this solution is suboptimal, because it is possible that a reader R1 might have the lock, and then another reader R2 requests access. It would be foolish for R2 to wait until R1 was done before starting its own read operation; instead, R2 should start right away. This is the motivation for the Reader Writer Lock pattern. Also known as multiple readers/single-writer lock or multi-reader lock or push lock Explanation Wikipedia Says In computer science, a readers�writer (RW) or shared-exclusive lock (also known as a multiple readers/single-writer lock or multi-reader lock or push lock) is a synchronization primitive that solves one of the readers�writers problems. An RW lock allows concurrent access for read-only operations, while write operations require...

Balking Pattern

Image
Intent Balking Pattern is used to prevent an object from executing certain code if it is an incomplete or inappropriate state. Wikipedia Says The balking pattern is a software design pattern that only executes an action on an object when the object is in a particular state. For example, if an object reads ZIPfiles and a calling method invokes a get method on the object when the ZIP file is not open, the object would "balk" at the request. In the Java programming language, for example, an IllegalStateException might be thrown under these circumstances. Source code In this example implementation, WashingMachine is an object that has two states in which it can be: ENABLED and WASHING. If the machine is ENABLED the state is changed into WASHING that any other thread can't invoke this action on this and then do the job. On the other hand, if it has been already washing and any other thread execute wash() it can't do that once again and returns doing nothing. Class Diagram ...