System concurrency Miscellaneous

Source: Internet
Author: User

 

Turn from: http://www.ibm.com/developerworks/cn/java/l-multithreading/ and add their own analysis, hope to provide a way of thinking for the design of the concurrent system.

First, let's take a simple example. There is a service provider in the system that provides external services through interfaces, such as printing Hello world.

// Define the public interface service {public void sayhello () ;}// implement public class serviceimp implements Service {public void sayhello () {system. out. println ("Hello world! ") ;}} // Interface caller public class client {public client (service s) {_ service = s;} public void requestservice () {_ service. sayhello ();} private service _ service;} // before the main program public class main {public static void main (string [] ARGs) {/* concurrency logic is added, method for calling the sayhello Service */service S = new serviceimp (); client c = new client (s); C. requestservice ();}}

If there are new requirements, the Service must support concurrent access from the client. A simple method is to add the synchronized Statement on the original interface, but the performance loss is quite large. This solution is basically destroyed (of course for this example, currently, it is unnecessary because serviceimp does not have data to be protected, but it may be available in the future as demand changes ). In this case, we need to change the original code at a high cost. The coupling of concurrency logic and application logic greatly compromises the readability and maintainability of the Code. If you can separate the concurrency logic from the application logic, the intrusion to the system will be greatly reduced, and the application logic itself can be reused. The major cause of client blocking, performance reduction, and inability to meet concurrency is that all service calls are synchronized. Now let's take a look at the asynchronous mode.

The core is to use active objects to encapsulate the concurrency logic and then forward client requests to the actual service provider (application logic ), in this way, both the client and the actual service provider do not need to care about the existence of concurrency, and do not need to consider the data consistency problems caused by concurrency. In this way, application logic and concurrency logic are isolated, and service calls and service execution are isolated. The following describes the key implementation details.

The framework consists of the following parts:

  1. An activeobject class, inherited from thread, encapsulates the activity object of the concurrency Logic
  2. An activequeue class mainly used to store caller requests
  3. A methodrequest interface is mainly used to encapsulate caller requests. It is an implementation method of the command design mode.

/*** Encapsulate the caller's request, that is, encapsulate an external interface */public interface methodrequest {public void call () ;}/ *** to store the caller's request, is the implementation of a concurrent stack */public class activequeue {public activequeue () {_ queue = new stack ();} public synchronized void enqueue (methodrequest Mr) {While (_ queue. size ()> queue_size) {try {Wait ();} catch (interruptedexception e) {e. printstacktrace () ;}}_ queue. push (MR); policyall (); system. out. println ("Leave queue");} public synchronized methodrequest dequeue () {methodrequest Mr; while (_ queue. empty () {try {system. out. println ("Getting started to get the queue"); wait ();} catch (interruptedexception e) {e. printstacktrace () ;}} mr = (methodrequest) _ queue. pop (); policyall (); return Mr;} private stack _ queue; private final static int queue_size = 20;}/*** thread object, encapsulated asynchronous concurrency logic */public class activeobject extends thread {public activeobject () {_ queue = new activequeue (); Start ();} public void enqueue (methodrequest Mr) {_ queue. enqueue (MR);} public void run () {While (true) {methodrequest mr = _ queue. dequeue (); Mr. call () ;}} private activequeue _ queue ;}
 
 

In fact, it is to use a new thread to fetch requests in the queue for execution, but all requests are encapsulated in the form of the methodrequest interface. In addition, the implementation of activequeue is inefficient, it can be implemented in the Java concurrent package. In this example, we first need to encapsulate the actual logic into methodrequest

public class SayHello implements MethodRequest {    public SayHello(Service s) {        _service = s;    }    public void call() {        _service.sayHello();    }    private Service _service;}

Next, of course, we need to encapsulate and queue requests. If the execution is well packaged, It is a unified portal. In order to be transparent to the client, this class must implement the service interface. Definition:

Public class serviceproxy implements Service {public serviceproxy () {_ service = new serviceimp (); // The thread listening queue _ active_object = new activeobject () is started here ();} public void sayhello () {methodrequest mr = new sayhello (_ service); _ active_object.enqueue (MR);} private service _ service; private activeobject _ active_object ;}

Other logic code has not changed. You can check the call before and after modification.

Public class main {public static void main (string [] ARGs) {/* Before the concurrency logic is added, call the sayhello Service * // service S = new serviceimp (); // client c = new client (s); // C. requestservice ();/* after the concurrency logic is added, the method for calling the sayhello Service */service S = new serviceproxy (); client c = new client (s); C. requestservice ();}}

We can see that the implementation of service in the application logic does not change. It simply implements the application logic and does not care about whether concurrency is required. Of course, this example is just a simple scenario. You only need to call and do not need to return the value. However, in many scenarios, the call needs to return the value. This is worth further consideration.

 

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.