Java Thread Communication Overview

Source: Internet
Author: User

The story comes from a very simple idea: Create a user-friendly, lightweight communication framework for developers, without locks, Synchronizer, semaphore, wait, and notification, to develop a lightweight, unlocked, intra-thread communication framework in Java; and no queues, no messages, Event or any other concurrency-specific term or tool.

Pojo communication is implemented using only ordinary, old-fashioned Java interfaces.

It may resemble Akka's typed actor, but as a new framework that must be super lightweight and optimized for a single multi-core computer, that might be a bit too much.

When actor spans process boundaries across different JVM instances (on the same machine, or across different machines on the network), the Akka framework is good at handling interprocess communication.

But for small projects that only require communication between threads, using Akka type actor may be a bit like killing a chicken with a sledgehammer, but typing actor is still an ideal way to achieve it.

It took me a few days to create a solution with dynamic proxies, blocking queues, and caching thread pools.

Figure I is a high-level architecture for this framework:

Figure I: A high-level architecture of the framework

SPSC queues refer to a single producer/single consumer queue. MPSC queues refer to multiple producers/single consumer queues.

The distribution thread is responsible for receiving messages sent by the actor thread and distributing them to the corresponding SPSC queue.

The actor thread that receives the message invokes the method in the corresponding actor instance with the data. With other actor proxies, the actor instance can send a message to the MPSC queue, and the message is sent to the target actor thread.

I created a simple example to test, which is the table tennis program below:

 public interface Playera void Pong (long ball);//The Forgotten method call} public interface Playerb {void ping (Playera Playera, long ball);    
  The Forgotten method call} public class Playeraimpl implements Playera {@Override public void pong (long ball) {     
    } public class Playerbimpl implements Playerb {@Override public void ping (Playera Playera, long ball) {    
  Playera.pong (ball); The public class Pingpongexample {public void Testpingpong () {//manager hides the complexity of communication between threads//control actor Proxy, actor implementation and line
    Cheng Actormanager manager = new Actormanager ();    
    Register actor Implementation Manager.registerimpl (Playeraimpl.class) within the manager;
    Manager.registerimpl (Playerbimpl.class); Create a actor agent. 
    The agent converts the method call into an internal message.    
    A specific actor instance is sent between threads.    
    Playera Playera = Manager.createactor (Playera.class);    
    Playerb Playerb = Manager.createactor (Playerb.class);     
   for (int i = 0; i < 1000000 i++) {playerb.ping (Playera, i); }    
}

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.