Implement the Active Object Mode in C #

Source: Internet
Author: User

In the previous section on active objects of ACE, I have briefly introduced the active object mode. Today I will briefly introduce the implementation of active objects in C.

The so-called active object is relative to the passive object. The method call and execution of the passive object are in the same thread, passive object method calls are synchronous and congested. Generally, objects belong to passive objects. The call and execution of active object methods are separated, the active object has its own independent execution thread. The method call of the active object is initiated by other threads, but the method is executed in its own thread, the call of the Active Object method is asynchronous and non-blocking.

The framework consists of the following parts:

    1. An activeobject class, inherited from thread, encapsulates the activity object of the concurrency Logic
    2. An activequeue class mainly used to store caller requests
    3. An imethodrequest interface is mainly used to encapsulate caller requests. It is an implementation method of the command design mode.

A simple implementation is as follows:

InterfaceImethodrequest
{
VoidCall ();
}

ClassActivequeue <t>
{
Const
IntMaxcount = 10;//Maximum

Queue <t> queue = New Queue <t> ();
Public
Void Enqueue (T item)
{
Lock ( This )
{
If (Queue. Count> = maxcount)
{
Monitor. Wait ( This );
}
Queue. enqueue (item );
Monitor. pulseall ( This );
}
}

Public T dequeue ()
{
Lock ( This )
{
If (Queue. Count = 0)
{
Monitor. Wait (This );
}
T item = queue. dequeue ();
Monitor. pulseall ( This );
Return Item;
}
}
}

ClassActiveobject
{
Activequeue <imethodrequest> queue =NewActivequeue <imethodrequest> ();

PublicActiveobject ()
{
Threadpool. queueuserworkitem (excutecommand );
}

Public
VoidAddcommand (imethodrequest cmd)
{
Cmdqueue. enqueue (CMD );
}

Private
VoidExcutecommand (Object OBJ)
{
While(True)
{
Imethodrequest cmd = queue. dequeue ();
Cmd. Call ();
}
}
}

In the three active object members activeobject, imethodrequest, and activequeue, the first two are relatively simple and easy to think of implementation methods. A little more complex is the activequeue object, which requires a thread-safe operation class. Here I use a simple encapsulation of the queue like Ace, in order to make it easy to use, I simply modified its enqueue and dequeue methods.

    1. When the enqueue method is used, if the number of elements in the queue has reached the maximum number limit, the elements in the queue will be removed by dequeue.
    2. When the enqueue method is used, if no available element exists in the queue, it will block other threads to insert the element into the queue through enqueue, and then return the available element.

I used monitor to implement thread synchronization. If you are interested, refer to it.

Active object is a common asynchronous communication mode. Here I simply provide a C # implementation. Of course, my examples here are not perfect. For more information, refer to the chapters I used to introduce active ace objects.

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.