Browser docs

Active Object

Intent

The active object design pattern decouples method execution from method invocation for objects that each reside in their thread of control. The goal is to introduce concurrency, by using asynchronous method invocation, and a scheduler for handling requests.

Explanation

The class that implements the active object pattern will contain a self-synchronization mechanism without using ‘synchronized’ methods.

Real-world example

The Orcs are known for their wildness and untameable soul. It seems like they have their own thread of control based on previous behavior.

To implement a creature that has its own thread of control mechanism and expose its API only and not the execution itself, we can use the Active Object pattern.

Programmatic Example

 1public abstract class ActiveCreature{
 2  private final Logger logger = LoggerFactory.getLogger(ActiveCreature.class.getName());
 3
 4  private BlockingQueue<Runnable> requests;
 5  
 6  private String name;
 7  
 8  private Thread thread;
 9
10  public ActiveCreature(String name) {
11    this.name = name;
12    this.requests = new LinkedBlockingQueue<Runnable>();
13    thread = new Thread(new Runnable() {
14        @Override
15        public void run() {
16          while (true) {
17            try {
18              requests.take().run();
19            } catch (InterruptedException e) { 
20              logger.error(e.getMessage());
21            }
22          }
23        }
24      }
25    );
26    thread.start();
27  }
28  
29  public void eat() throws InterruptedException {
30    requests.put(new Runnable() {
31        @Override
32        public void run() { 
33          logger.info("{} is eating!",name());
34          logger.info("{} has finished eating!",name());
35        }
36      }
37    );
38  }
39
40  public void roam() throws InterruptedException {
41    requests.put(new Runnable() {
42        @Override
43        public void run() { 
44          logger.info("{} has started to roam the wastelands.",name());
45        }
46      }
47    );
48  }
49  
50  public String name() {
51    return this.name;
52  }
53}

We can see that any class that will extend the ActiveCreature class will have its own thread of control to invoke and execute methods.

For example, the Orc class:

1public class Orc extends ActiveCreature {
2
3  public Orc(String name) {
4    super(name);
5  }
6
7}

Now, we can create multiple creatures such as Orcs, tell them to eat and roam, and they will execute it on their own thread of control:

 1  public static void main(String[] args) {  
 2    var app = new App();
 3    app.run();
 4  }
 5  
 6  @Override
 7  public void run() {
 8    ActiveCreature creature;
 9    try {
10      for (int i = 0;i < creatures;i++) {
11        creature = new Orc(Orc.class.getSimpleName().toString() + i);
12        creature.eat();
13        creature.roam();
14      }
15      Thread.sleep(1000);
16    } catch (InterruptedException e) {
17      logger.error(e.getMessage());
18    }
19    Runtime.getRuntime().exit(1);
20  }

Class diagram

alt text

Tutorials