WEB开发网
开发学院软件开发Java Active Object 并发模式在 Java 中的应用 阅读

Active Object 并发模式在 Java 中的应用

 2010-08-06 00:00:00 来源:WEB开发网   
核心提示: classMethod_Request{public://Evaluatethesynchronizationconstraint.virtualboolcan_run()const=0//Executethemethod.virtualvoidcall()=0;};//Inheritesfro

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 the queue implementation, 
     // e.g., an array or a linked list, go here. 
}; 

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:Active Object 并发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接