Active object

1

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

Example

Java

An example of active object pattern in Java. 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. 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. To rewrite this class as an active object, you could do the following:

Java 8 (alternative)

Another example of active object pattern in Java instead implemented in Java 8 providing a shorter solution.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article