Concurrency Pattern: Active object pattern
Active object pattern

Concurrency Pattern: Active object pattern

This pattern is a type of concurrency design pattern and widely used in applications. Also, it is used to create concurrent APIs like ExecutorService in Java.

If we go in the past maybe a decade, if we have to upload a file in Yahoo email or Gmail then we have to wait till the file is uploaded before moving to any other work. Your screen was locked till the moment the file was uploading. Why it has happened? Bec of Single thread. Your email was running on a single thread which blocks all other requests till uploading completed.

But if we compare it now, we can do multiple tasks simultaneously. How does it happen?

Thanks to the Active Object pattern which actually helps to overcome this issue. How does this pattern work? Let us discuss this first.

Active Object is a concurrency pattern in which we try to separate the invocation of a method from its execution.

The method invocation is made on an Active Object on the client thread and method execution is performed by an independent thread asynchronously without blocking the client thread. Thus, the client thread is not tied-up until the method execution is over. After a method is invoked and the command to execute it is dispatched to the Scheduler, it can perform other tasks.

No alt text provided for this image


The key elements in active object pattern are:

  • Proxy (or Client Interface) - A public method provided by the active object to clients. An active object creates a message which contains information about the method call and puts into a queue
  • Dispatch Queue - A list of pending requests from clients. (Unbounded (LinkedBlocking queue) or Bounded (Blocking queue))
  • Scheduler - Run independently on a separate thread and dequeue the message from the queue. Once the message is read, Scheduler will create a one or multiple threads called as Servant to execute the method (message)
  • Result Handle (or Callback) - This allows the result to be obtained by proxy after a request is executed.

Example of this pattern I have mentioned in the previous post here.

I hope it helps how this pattern works. Understanding this pattern helps to understand how ExecutorService works internally and also to build a multithreaded responsive application.

要查看或添加评论,请登录

Tushar Goel的更多文章

  • DB Isolation Levels Part -2 (Repeatable Read and Serializable)

    DB Isolation Levels Part -2 (Repeatable Read and Serializable)

    In the last article, we learned about Read un-committed and Read committed isolation levels and the problems linked…

  • DB Isolation Levels in Detail - Part -1

    DB Isolation Levels in Detail - Part -1

    If two transactions don't touch the same data, they can safely run in parallel because they are not dependent on each…

    1 条评论
  • How Kafka is so efficient?

    How Kafka is so efficient?

    What is Apache Kafka? Apache Kafka is a distributed streaming platform that allows you to:: Publish and subscribe to…

  • Dependency Inversion

    Dependency Inversion

    Dependency ingestion is ‘D’ in the SOLID design principle. It is a guideline that helps design loosely coupled classes.

  • Law of Demeter (Principle of least knowledge)

    Law of Demeter (Principle of least knowledge)

    In the last article we have discussed Tell, Don’t ask guideline. In this article, we will discuss another guideline…

  • Tell, Don't ask!

    Tell, Don't ask!

    Few days back I was having a discussion with one of my friends and we were discussing how to reduce coupling between…

  • JavaScript: DOM Manipulation

    JavaScript: DOM Manipulation

    In this article, I will explain how to do DOM manipulation using pure JavaScript. We have many other libraries…

  • The JavaScript EventLoop

    The JavaScript EventLoop

    This post will explain one of the most important concepts of JavaScript i.e.

  • How to use an index in MongoDB?

    How to use an index in MongoDB?

    In this post, I will explain how to use an index in MongoDB. In an earlier article, I explained how indexes work.

    3 条评论

社区洞察

其他会员也浏览了