Disclaimer: I am talking about ActiveMQ Artemis internals here, giving tips on how you could achieve something similar on any system.

Systems today make heavy usage of Executors. No matter what system you chose, they are always there... Executors everywhere.

One pitfall they usually bring is: you will have to create one Runnable every time you call the executor:

Executor executor; // pretend this is already initialized

String data = "Hello world";
executor.execute(new Runnable() {
    System.out.println(data);
});

If you call this millions of time, you will have lots of Runnable instantiated that will need to be cleared by the Garbage Collector. (poor guy)

To make it easier, we haven recently introduce a new type of Executor on ActiveMQ Artemis internals. With the usage of lambdas it gets really elegant:

Executor parentExecutor; // pretend this is already initialized
Actor actor = new Actor<>(parentExecutor, ::onMessage);

// this will call onMessage..
// an executor will be used underneath but no new Runnables are created
actor.act("Hello world");

public void onMessage(String message) {
   System.out.println(message);
}

Look at the code yourself if you want to have a similar pattern on your system:

- Our commons packet

- Code in use