The active object design pattern decouples method execution from method invocation for objects that each reside in their own thread of control. [1] The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests. [2]
The pattern consists of six elements: [3]
An example of active object pattern in Java. [4]
Firstly we can see a standard class that provides two methods that set a double to be a certain value. This class does NOT conform to the active object pattern.
classMyClass{privatedoubleval=0.0;voiddoSomething(){val=1.0;}voiddoSomethingElse(){val=2.0;}}
The class is dangerous in a multithreading scenario because both methods can be called simultaneously, so the value of val (which is not atomic—it's updated in multiple steps) could be undefined—a classic race condition. You can, of course, use synchronization to solve this problem, which in this trivial case is easy. But once the class becomes realistically complex, synchronization can become very difficult. [5]
To rewrite this class as an active object, you could do the following:
classMyActiveObject{privatedoubleval=0.0;privateBlockingQueue<Runnable>dispatchQueue=newLinkedBlockingQueue<Runnable>();publicMyActiveObject(){newThread(newRunnable(){@Overridepublicvoidrun(){try{while(true){dispatchQueue.take().run();}}catch(InterruptedExceptione){// okay, just terminate the dispatcher}}}).start();}voiddoSomething()throwsInterruptedException{dispatchQueue.put(newRunnable(){@Overridepublicvoidrun(){val=1.0;}});}voiddoSomethingElse()throwsInterruptedException{dispatchQueue.put(newRunnable(){@Overridepublicvoidrun(){val=2.0;}});}}
Another example of active object pattern using Java 8 features.
publicclassMyClass{privatedoubleval;// container for tasks// decides which request to execute next // asyncMode=true means our worker thread processes its local task queue in the FIFO order // only single thread may modify internal stateprivatefinalForkJoinPoolfj=newForkJoinPool(1,ForkJoinPool.defaultForkJoinWorkerThreadFactory,null,true);// implementation of active object methodpublicvoiddoSomething()throwsInterruptedException{fj.execute(()->{val=1.0;});}// implementation of active object methodpublicvoiddoSomethingElse()throwsInterruptedException{fj.execute(()->{val=2.0;});}}