To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

From Wikipedia, the free encyclopedia

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]

  • A proxy, which provides an interface towards clients with publicly accessible methods.
  • An interface which defines the method request on an active object.
  • A list of pending requests from clients.
  • A scheduler, which decides which request to execute next.
  • The implementation of the active object method.
  • A callback or variable for the client to receive the result.

Example

Java

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.

class MyClass {

    private double val = 0.0;
    
    void doSomething() {
        val = 1.0;
    }

    void doSomethingElse() {
        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:

class MyActiveObject {

    private double val = 0.0;
    private BlockingQueue<Runnable> dispatchQueue = new LinkedBlockingQueue<Runnable>();

    public MyActiveObject() {
        new Thread (new Runnable() {
                    
                @Override
                public void run() {
                    try {
                        while (true) {
                            dispatchQueue.take().run();
                        }
                    } catch (InterruptedException e) {   
                        // okay, just terminate the dispatcher
                    }
                }
            }
        ).start();
    }

    void doSomething() throws InterruptedException {
        dispatchQueue.put(new Runnable() {
                @Override
                public void run() { 
                    val = 1.0; 
                }
            }
        );
    }

    void doSomethingElse() throws InterruptedException {
        dispatchQueue.put(new Runnable() {
                @Override
                public void run() { 
                    val = 2.0; 
                }
            }
        );
    }
}

Java 8 (alternative)

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

public class MyClass {
    private double val; 
    
    // 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 state
    private final ForkJoinPool fj = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
    
    // implementation of active object method
    public void doSomething() throws InterruptedException {
        fj.execute(() -> { val = 1.0; });
    }
 
    // implementation of active object method
    public void doSomethingElse() throws InterruptedException {
        fj.execute(() -> { val = 2.0; });
    }
}

See also

References

  1. ^ Douglas C. Schmidt; Michael Stal; Hans Rohnert; Frank Buschmann (2000). Pattern-Oriented Software Architecture, Volume 2: Patterns for Concurrent and Networked Objects. John Wiley & Sons. ISBN 0-471-60695-2.
  2. ^ Bass, L., Clements, P., Kazman, R. Software Architecture in Practice. Addison Wesley, 2003
  3. ^ Lavender, R. Greg; Schmidt, Douglas C. "Active Object" (PDF). Archived from the original (PDF) on 2012-07-22. Retrieved 2007-02-02.
  4. ^ Holub, Allen. "Java Active Objects - A Proposal". Archived from the original on 2013-06-22. Retrieved 2014-06-16.
  5. ^ Holub, Allen. "Java Active Objects - A Proposal". Archived from the original on 2013-06-22. Retrieved 2014-06-16.

External links


This page was last edited on 26 March 2024, at 21:31
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.