Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Active object

From Wikipedia, the free encyclopedia
This article is about a multi-threading technique. For thelockstep protocol variant, seeActive objects.

Theactive objectdesign pattern decouples method execution from method invocation for objects that each reside in their ownthread of control.[1] The goal is to introduceconcurrency, by usingasynchronous method invocation and ascheduler for handling requests.[2]

The pattern consists of six elements:[3]

  • Aproxy, 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.
  • Ascheduler, which decides which request to execute next.
  • The implementation of the active object method.
  • Acallback orvariable for the client to receive the result.

Example

[edit]

Java

[edit]

An example of active object pattern inJava.[4]

Firstly we can see a standard class that provides two methods that set a double to be a certain value. This class doesNOT 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;});}}

See also

[edit]

References

[edit]
  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 fromthe original(PDF) on 2012-07-22. Retrieved2007-02-02.
  4. ^Holub, Allen."Java Active Objects - A Proposal". Archived fromthe original on 2013-06-22. Retrieved2014-06-16.
  5. ^Holub, Allen."Java Active Objects - A Proposal". Archived fromthe original on 2013-06-22. Retrieved2014-06-16.

External links

[edit]
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also


Stub icon

Thiscomputer-programming-related article is astub. You can help Wikipedia byadding missing information.

Retrieved from "https://en.wikipedia.org/w/index.php?title=Active_object&oldid=1309376180"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp