Because of the recent work relationship, you want to split the asynchronous task from the application server into a dedicated asynchronous processing server.
Programme I
is to use MQ to send a task message to the server for processing, as shown in:
This scheme uses MQ as the intermediate medium, using the thread pool asynchronous processing task on the server side, after processing completes sends the result to the MQ, the client listens the way obtains the result to continue processing.
The disadvantage of this scenario is that it is possible to store the results in a shared hashmap or threadlocal in the case of certain requirements, and the client blocks until the results are obtained, in the context of multithreading, or with shared variables, although shared variables may be thread-safe, But from the perspective of the concurrency model, it's not the best way.
Programme II
Use the more popular Akka framework to achieve.
Five characteristics of Akka
* Easy to build parallel and distributed applications
* Reliability (Resilient by Design)
The system has self-healing capability and is supervised locally/remotely.
* High Performance (performance)
You can send 50 million messages per second in a single machine. The memory footprint is small, and 2.5 million actors can be saved in 1GB memory.
* Elasticity, no center (elastic-decentralized)
Adaptive responsible Equalization, routing, partitioning, configuration
* Extensible (extensible) can be extended using the Akka expansion pack.
Because the performance of multithreading that has been studied Scala,scala is very high, the Akka framework developed based on the Scala language has been widely used. Then I'll use a very simple example, and some test cases to show its performance.
The code is as follows:
ImportAkka.actor.ActorRef;ImportAkka.actor.ActorSystem;ImportAkka.actor.Props;ImportAkka.actor.UntypedActor;/** * Project_name:akkademo * DATE:16/2/27 * CREATE BY:chao.cheng **/ Public class tostringactor extends untypedactor { @Override Public void OnReceive(Object message) {System.out.println (message.tostring ());Try{Thread.Sleep ( -); }Catch(Exception e) {E.printstacktrace (); } } Public Static void Main(string[] args) {Actorsystem system = Actorsystem.create ("Tostringactor");FinalActorref toString = system.actorof (Props.create (Tostringactor.class),"ToString"); for(intI=0;i<10000000; i++) {Tostring.tell ("Test"+i,tostring); } System.out.println ("[End]=======================]); }}
Simple description of the program:
Using the mechanism of the event, the loop sends 10 million data and asynchronously processes the task through the OnReceive method.
With the VISUALVM tool you can see:
Background in fact, adaptive only up to three threads running, respectively, is dispatcher-2,dispatcher-3,dispatcher-4.
Analyzing performance time-consuming.
The total CPU time for the server is 15.9%.
Akka Simple Performance Test