Model-oriented software architecture 2-concurrent and networked object model Reading Notes (14)-concurrent mode and active object

Source: Internet
Author: User

Concurrency Mode

Two models designed to share resources among multiple threads or processes:

· The Active Object design mode separates method execution from method calls. The purpose is to enhance concurrency and simplify synchronous access to objects residing in the control thread.

· The monitor Object Design Pattern synchronizes the execution of Concurrent methods to ensure that there is only one method running within the object at the same time. It also allows the execution sequence of Collaborative Scheduling Methods of objects.

 

The following two modes define the high-level concurrency architecture:

· The semi-synchronous/semi-asynchronous architecture separates synchronous and asynchronous processes in the concurrent system, simplifying programming without affecting performance. This mode introduces two communication layers, one processing Synchronization Service and the other processing asynchronous service. There is also a queuing layer that coordinates the communication between the asynchronous layer and the synchronization layer service.

· The leader/follower architecture model provides an efficient concurrency model. multiple threads share a group of event sources in turn, in this way, service requests generated in the event source are detected, decomposed, allocated, and processed. When requests processed by the buffer pool threads are not synchronized or there is no sort constraint, the leader/follower mode can replace the semi-synchronous/semi-asynchronous and active object modes to improve performance.

 

Another policy is provided to solve the inherent complexity of some concurrency:

· The thread-specific memory design pattern allows multiple threads to use a "logically global" access point to retrieve a local object without causing a lock and unlock when accessing the object. To some extent, this mode can be seen as the "Opposite" of other models, because it achieves a complicated internal concurrency mechanism by preventing resource sharing between processes.

 

5.1 active object)

1. Problem

Many applicationsProgramThe service quality is improved by using concurrent objects. However, if these objects are shared and modified by multiple client threads during concurrent object running, the methods and data access must be synchronized, and three mandatory conditions are generated:

1) concurrently calling the processing intensive methods of objects should not block the entire process indefinitely, and reduce the quality of service for other concurrent objects.

2) It should be easy to program synchronous access to shared objects. In particular, the serialization and scheduling of client method calls of shared objects should be transparent, and the shared objects have synchronization constraints.

3) applications should be designed transparently using the parallel mechanism provided on the hardware/software platform.

 

2. Solution

For objects restricted by the preceding mandatory conditions, separate the method call from the execution. The method call should occur in the control thread of the client, and the method execution should occur in another thread. In addition, we need to design this separation to make the client thread look like calling a common method.

Details: proxy represents the interface of the active object. The service provider (Servant) provides the implementation of active objects. The proxy and the service provider run in different threads, so the method call and method execution can be executed concurrently. The agent runs in the client thread, while the service provider runs in another thread.

In the runtime era, the client's method call is converted to a method request, and the scheduler stores the method request in an activation table (activation list. The event loop of the scheduler and the service provider run continuously in the same thread. The method requests are retrieved from the activation table queue and allocated to the service provider. The client can obtain the result of method execution through the foreground returned by the proxy.

 

3. Structure

The proxy provides an interface that allows the client to call public accessible methods of active objects. The use of proxies allows the program to use standard strong language features for programming, rather than passing weak messages between threads. The agent resides in the client thread.

When a client calls a method defined by a proxy, the constructor Method Request of the Method Request object includes context information (such as method parameters ), this information is required when you execute a specified method call and return results to the client. The Method Request class defines an interface for executing the active object method. The interface also includes the Sentinel method that can be used to determine whether a method request can be executed. For each public method provided by the proxy in the active object that requires synchronous access, a specific method request class (concrete methodrequest) is generated from the Method Request class ).

 

 

The proxy inserts an activation table (activation list) into the request for specific methods it creates ). This table contains a limited buffer consisting of method requests established by the proxy to be processed and tracks which method requests are executable. The activation table separates the client threads that reside in the proxy from the threads that execute the service, so that the two threads can run simultaneously. Therefore, the internal status of the activated table must be serialized to prevent simultaneous access to the table.

Sched runs in a thread other than its client proxy, that is, the thread of the active object. It determines which method request will be executed for the next active object. This scheduling decision is based on different criteria (such as sorting the order of an active object call method) or some attributes of the active object (such as its status ). The scheduler can evaluate these attributes by Using the Method Request sentry to determine when these method requests can be executed. The scheduler uses the activation table to manage the method requests to be processed. When the client calls its method, the proxy inserts the Method Request into the activation table.

A service provider is modeled as an active object, which defines its behavior and status. The method implemented by the service provider corresponds to the proxy interface and the Method Request created by the proxy. The service provider may also include other predicate methods that can be used by method requests to implement the Sentinel. The service provider method is called when the scheduler executes the associated Method Request. Therefore, it is executed in the thread of the scheduler.

When the client calls a method on the proxy, it will receive a future ). This prospect allows the client to obtain method call results after the service provider completes method execution. Each foreground reserves a certain amount of space for the called method and stores the result of the method call. When the client wants this result, it can converge with the foreground (rendezvous), block or round-robin until the result is calculated and saved to the foreground.

 

4. Implementation

1) implement the service provider. A service provider is modeled as an active object, which defines its behavior and status. In addition, the service provider may also include a predicate method used to determine when to execute a Method Request.

2) Infrastructure for invocation. This activity describes the infrastructure required for the client to call the method of the active object. This infrastructure is builtCubeThe proxy of the request.

2.1) Implement proxy. The proxy provides an interface for the client to access the service provider. The proxy creates a specific method request for each method call of the client. Each method request is an abstraction of the method context, also known as the method closure (closure ). Generally, this context includes the method parameters, binding to the service provider of the method application, the foreground of the result, andCode.

2.2) implement method requests. A Method request can be viewed as a command object. The Method Request class declares an interface used by all specific method requests. It provides a unified interface for the scheduler, you do not need to know how to evaluate synchronization constraints or trigger the execution of specific method requests.

3) activate the table. Each method request is inserted into an activation table. This table can be implemented as a synchronization limit buffer, which is shared by the client thread and the thread that runs the active object scheduler and the service provider. The activation table can also provide a robust iterator, And the scheduler can traverse and delete its elements.

4) Implement the scheduling program of the active object. A scheduler is a command handler. Used to manage the activation table and execute unprocessed method requests that meet the synchronization constraints. The common interface of the scheduler usually provides two methods, one of which is used by the proxy to insert the Method Request to the activation table. The other is used to allocate method requests to the service provider.

5) determine the convergence and return value policies. The convergence policy determines how the client obtains the return value from the method called for the active object. When the active object service provider executed in one thread transmits the return value to the client program that calls this method in another thread, convergence occurs. The following convergence and return value policies are usually used to implement the Active Object Mode:

· Synchronization wait. In the proxy, the client thread is blocked synchronously until the scheduler allocates method requests and obtains results and stores them in the foreground.

· Timed synchronization wait. Blocking for a limited time, and failure if the scheduling program of the active object fails to allocate method requests within the specified time. If the time-out value is zero, the client thread will "Poll", that is, when the Scheduler cannot allocate it immediately, it can return to the caller without putting the Method Request into the queue.

· Asynchronous. Put the method call into the queue and immediately return the control to the client. If the method is a two-way call that generates results, you must use some form of foreground to provide synchronous access to the value, or synchronous access to the error state when the call fails.

 

5. Conclusion

Advantages:

1) enhances the concurrency of applications and simplifies the synchronization complexity. Concurrency of applications is increased by allowing concurrent execution of client threads and asynchronous methods. By using the scheduling program, the synchronization complexity is simplified. The scheduling program calculates synchronization constraints to ensure that the service provider can perform serial access based on its status.

2) transparent application available concurrency. If the hardware and software platforms can effectively support multiple CPUs, this mode allows multiple active objects to be executed in parallel. Whether parallel execution depends only on their synchronization constraints.

3) The execution sequence of methods can be different from that of methods. The asynchronous call method is executed based on the synchronization mechanism defined by the method's Sentel and scheduling policy. Therefore, the execution sequence of methods can be different from that of method calls. This separation helps improve the performance and flexibility of applications.

 

Disadvantages:

1) performance overhead. Depending on the implementation methods of the scheduling program of the active object, context switching, synchronization, and data movement overhead may occur when the scheduling and execution of the Active Object method call. Generally, the active object mode is most suitable for objects with relatively coarse granularity.

2) complex debugging. Because of the concurrency and uncertainty of various active object scheduling programs and underlying operating system Thread Scheduling programs. Therefore, it is difficult to debug programs that use the active object mode.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.