Application of the Active object concurrency pattern in Java

Source: Internet
Author: User
Tags execution thread

Introduction: Active object is a typical design pattern in concurrent programming practice, and the core of active object mode is to improve the concurrency of programs by solving the invocation and execution of the coupling method. This paper begins with the typical active object design pattern and explores the application of the active object concurrency pattern in Java from a new perspective.

This article mainly from the following two aspects of the elaboration:

Use the C + + language to describe the Active Object design pattern.

Java class libraries are a good repository-level encapsulation of such a typical pattern, so for Java developers, much of the design pattern itself is blocked. This article attempts to use the Native C + + language to help readers to have a more comprehensive understanding of Active Object Design patterns in nature.

Combining the C + + version of the active object design pattern leads readers to a deeper understanding of the active object design pattern's support in the Java class library, helping readers to use these libraries correctly and effectively.

Preliminary knowledge

Concurrent objects (Concurrent object)

Here, let's first define what a concurrent object is. Unlike a generic object, a concurrent object means that the invocation of the object method is not Chengne the same line as the execution of the method, or that the object method is executed asynchronously. This is actually a typical computational feature in a multithreaded programming environment, where threads introduce asynchrony. From another point of view, the concurrent object is actually the object-oriented concept applied to the product under the Multithread computing environment.

Active Object Design Mode C + + description

We will discuss Active Object mode in the following ways.

Problem description

We all know that concurrent programming patterns based on multithreaded mechanisms can greatly improve the QoS of applications (Quality of Service). Typical example, when we develop server-side applications, the common practice is to use multithreaded mechanism to service clients to submit requests, in order to improve the server response to the client (responsiveness). At the same time, concurrent multi-threaded applications are much more complex than single-threaded applications. In a multithreaded computing environment, concurrent objects are shared by all caller threads. In general, the design implementation of concurrent objects needs to consider several important factors:

Any method execution of a concurrent object, which does not allow unlimited or long time to block calls from other methods, affects the QoS of the application.

Because concurrent objects are shared by the caller thread, their internal state must be thread-safe and must be restricted to certain thread synchronization constraints, and these constraints are transparent and invisible to the caller. From the caller's point of view, concurrent objects are not much different from normal objects.

The design implementation of concurrent objects should be able to transparently utilize the concurrency mechanism provided by the underlying platform. The advantage of this is that when concurrent objects are running on the underlying hardware platform such as multi-core processors, our application can fully exploit the concurrency advantages of the underlying platform to get good application performance.

We use the Active Object design pattern to solve these problems.

The nature of the Active Object design pattern is the invocation of the Decoupled method (methods invocation) and the execution of the method (methods execution), the method invocation takes place in the caller thread context, and the execution of the method occurs in the object independent of the caller thread. In the Active Object thread context. And the important point is that this method is no different from other ordinary object member methods to the caller. From the run-time point of view, there are two types of threads, one is the caller thread, the other is an Active Object thread, which we'll talk about in more detail below.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.