请输入您要查询的百科知识:

 

词条 Active object
释义

  1. Example

      Java    Java 8 (alternative)  

  2. See also

  3. References

  4. External links

{{About|a multi-threading technique|the lockstep protocol variant|Active objects}}

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 {

    public MyActiveObject() {        new Thread (new Runnable() {                                    @Override                public void run() {                    while(true) {                        try {                            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

  • Concurrent object-oriented programming
  • Actor model
  • Futures and promises
  • Live distributed object

References

1. ^{{cite book | author = Douglas C. Schmidt |author2=Michael Stal|author3=Hans Rohnert|author4=Frank Buschmann | year = 2000 | title = Pattern-Oriented Software Architecture, Volume 2: Patterns for Concurrent and Networked Objects | publisher = John Wiley & Sons | isbn = 0-471-60695-2}}
2. ^Bass, L., Clements, P., Kazman, R. Software Architecture in Practice. Addison Wesley, 2003
3. ^{{cite web | title = Active Object | first = R. Greg | last = Lavender | author2= Schmidt, Douglas C. | format = PDF | url = http://www.cs.wustl.edu/~schmidt/PDF/Act-Obj.pdf | accessdate=2007-02-02}}
4. ^{{cite web | title = Java Active Objects - A Proposal | first = Allen | last = Holub | url = http://pragprog.com/magazines/2013-05/java-active-objects | accessdate=2014-06-16}}
5. ^{{cite web | title = Java Active Objects - A Proposal | first = Allen | last = Holub | url = http://pragprog.com/magazines/2013-05/java-active-objects | accessdate=2014-06-16}}

External links

  • [https://github.com/lightful/syscpp/ Active Object implementation in C++11]
{{Design Patterns patterns}}{{Compu-prog-stub}}

3 : Software design patterns|Concurrency (computer science)|Articles with example Java code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/21 22:14:42