Sunday, October 29, 2017

Paratrooper Model

In my previous entry, I was describing the Rocket Anti-Pattern, where one thread would handle too much work across several application layers, leading to communication buffer overflow and message loss.
 paratrooper.gifTo solve this problem, you can use the Paratrooper Model. When paratroopers jump from a plane, they are not waiting for the previous guy who jumped to land safely on the ground before jumping themselves. They are jumping as soon as they can after the guy that precedes them left the place. It should be the same with your data. The thread that receives and transcodes the messages should only do that and nothing else. It should work as fast as possible, and let another thread go on with the work in the next application layer, while concentrating on the next message. This way, we are building a sort of pipeline, with one or several worker threads in each application layer.
Here is an equivalent of the application shown in the previous entry, using our new model:
public class Comm {
    public void onMessage(Message m) {
        Data data = transcode(m);
 calculator.push(data);
    }
}

public class Calculator {
    public void push(Data data) {
executor.execute(() -> calculatePrice(data));
    }

    public void calculatePrice(Data data) {
        data.price = data.unitPrice * data.quantity;
 Display.show(data);
    }
}

public class Display {
    public void show(Data data) {
SwingUtilities.invokeLater(() -> table.addRow(data));
    }
}

Each class represents a layer, and propagates the data to the next layer, which will in turn ask the layer’s dedicated thread to handle it.
To implement this model, you will have to use the Marsupilami Pattern. Wait for it!

Saturday, October 28, 2017

Rocket Anti-Pattern

At my presentation at Devoxx Paris this year, The Thread Awakens, I introduced several Patterns related to multithreading. Since it was a quickie, I had only 15 minutes to talk about 15 Patterns. That got me to think: maybe some people would like me to develop a bit more on them. Or maybe some other people would prefer to read about them than to watch me on video (I, for instance, would not watch myself on video…). Or, maybe more surprisingly, some people do not speak french (it might happen).
So for all those people, I will introduce my Patterns in my blog, one at a time. And I’ll start with the Rocket Anti-Pattern.
rocket.gifData in an application travels like a rocket. It crosses several application layers in the same way that rockets cross several layers of atmosphere before reaching space. Data might enter through a database layer, or a communication layer, then cross one or several business layers, before leaving the application through another communication layer, or a user interface. But sometimes, the analogy goes a bit too far
When you are watching a rocket leaving, you follow it from the moment it ignites to the moment it disappear into the sky. The Java equivalent would be:
public class Comm {
    public void onMessage(Message m) {
        Data data = transcode(m);
        calculator.calculatePrice(data);
        Display.show(data);
    }
}

public class Calculator {
    public void calculatePrice(Data data) {
        data.price = data.unitPrice * data;quantity;
    }
}

public class Display {
    public void show(Data data) {
        SwingUtilities.invokeAndWait(table::addRow);
    }
}

What you see here, is a Message that is transcoded into a Data class, then a price is calculated, and finally, Data is displayed on the user interface. You’ll notice the use of SwingUtilities.invokeAndWait(), which might be too much, but you’ll get the point: the same thread is doing almost everything, and won’t return before data has crossed all the layers.
The drawback of this method appears if you get many messages. In a usual communication layers, messages will fill a buffer, where they’ll wait to be handled. If the handling thread is too slow (like when it waits for data to cross all the layers), buffer will get overflod, and you’ll start to loose messages. And that’s not what you want.
If you wish to avoid the Rocket Anti-Pattern, you’ll need to use the Paratrooper Model, which I’ll describe in my next entry. Stay tuned!

Thursday, October 26, 2017

Moved away

I got very lazy, and left my blog on JRoller for too long. Now that it's dead, I lost everything.
But never mind, let's start on a new page with this blog here. See you soon with interesting articles on Java!