e object.
void put (const Message &msg) {
Method_Request *mr = new Put(servant_,msg);
scheduler_.insert (mr);
}
// Return a <Message_Future> as the "future" result of
// an asynchronous <get> method on the active object.
Message_Future get () {
Message_Future result;
Method_Request *mr = new Get (&servant_,result);
scheduler_.insert (mr);
return result;
}
// empty() and full() predicate implementations ...
private:
// The servant that implements the active object
// methods and a scheduler for the message queue.
MQ_Servant servant_;
MQ_Scheduler scheduler_;
};
Active Object并发模式在Java中的应用(4)
时间:2011-10-22 IBM 李三红
同一个进程中的多个调用者线程可以共享同一个 Proxy。
实现 Method Request,如清单 3 所示:
清单 3. Method_Request
class Method_Request {
public:
// Evaluate the synchronization constraint.
virtual bool can_run () const = 0
// Execute the method.
virtual void call () = 0;
};
// Inherites from Method_Request
class Get : public Method_Request {
public:
Get (MQ_Servant *rep, const Message_Future &f)
:servant_ (rep),
result_ (f)
{
}
virtual bool can_run () const {
// Synchronization constraint: cannot call a
// <get> method until queue is not empty.
return !servant_->empty ();
}
virtual void call () {
// Bind dequeued message to the future result.
result_ = servant_->get ();
}
private:
MQ_Servant *servant_;
Message_Future result_;
};
实现 Activation List,如清单 4 所示:
清单 4. Activation_List
class Activation_List {
public:
// Block for an "infinite" amount of time waiting
// for <insert> and <remove> methods to complete.
enum { INFINITE = -1 };
// Define a "trait".
typedef Activation_List_Iterator iterator;
Activation_List ();
// Insert <method_request> into the list, waiting up
// to <timeout> amount of time for space to become
// available in the queue. Throws the <System_Ex>
// exception if <timeout> expires.
void insert (Method_Request *method_request,Time_Value *timeout = 0);
// Remove <method_request> from the list, waiting up
// to <timeout> amount of time for a <method_request>
// to be inserted into the list. Throws the
// <System_Ex> exception if <timeout> expires.
void remove (Method_Request *&method_request, Time_Value *timeout = 0);
private:
// Synchronization mechanisms, e.g., condition
// variables and mutexes, and
|