Quantcast
Channel: Scratch Where It's Itching
Viewing all articles
Browse latest Browse all 73

Marsupilami Pattern

$
0
0
In my previous entry, I described the Paratrooper Model, where each application layer would have its own thread pool for handling data. And the way to implement that is to use the Marsupilami Pattern.

It might be that you haven’t heard of the Marsupilami. You have to know that there are three countries in the world where comic strips are akin to a cult: the US with Marvel and DC Comics super heroes, Japan with their mangas, and France. Growing up in France, my childhood was populated with comics characters such as Asterix, Tintin, Lucky Luke, or the Smurfs. Or the Marsupilami, a creature half way between the jaguar and the monkey, living in the Amazonian forest and who solves all his problems using a very long tail.

That makes for a nice pun in French, since we use the same word for tail and for queue, and here, we will solve our Paratrooper Model problem by introducing a queue. Each application layer is separated from the next one by a queue. When data is handled, we just push it to a queue, from which next layer’s threads will draw, while we can concentrate on our next data.

In Java, this is really easy. If we go back to our Calculator class from last time:

public class Calculator {private final Executor executor = Executors.newFixedThreadPool(5);public void push(Data data) {
        executor.execute(() -> calculatePrice(data));
    }public void calculatePrice(Data data) {
        data.price = data.unitPrice * data.quantity;
        Display.show(data);
    }
}

The important line here is the one where we create a new Executor. In Java, with this one line, you create two things: a thread pool, and a queue (the Marsupilami’s tail).

But beware, if you do not pay enough attention, you might end up with the Indian Train Anti-Pattern. Stay tuned!


Viewing all articles
Browse latest Browse all 73

Trending Articles