Disrupt java threads.

Hello,

First, keep calm and keep reading :), there is no c/c++ code there, only pure and clear Java code 😀
By disrupting java threads, i mean the way we use them to communicate with each others.

Basically, when you want to enable communication between threads, the first idea is to use a queue : Thread-producer create a workload, put it in a queue to be consumed by Thread-consumer.

Easy to understand, but not efficient if we want to :

    • Have multiple consumers : like a multicast queue
    • To build a topologie (dependecy graph) can be a nightmare of complexity
    • Lock-free : Queues in java are ‘Blocking’ because the use locks, so we can have performance issues there.

So, in this article i will show you how to build a complex inter-thread messaging architecture in an EASY way 🙂

Let me introduce Disruptor framework from LMAX (a trading platform)

Disruptor is a very low-latency and high-throughput inter-thread messaging framework based on standard java framework (no ext lib ^_^) and on the RingBuffer pattern (Circular buffer from wikipedia or more human explanation from Trisha Gee here).

So the Disruptor is more than an implementation of Queue in java, it provides :

  • Multicast events to consumers, with consumer dependency graph.
  • Pre-allocate memory for events.
  • Optionally lock-free.

Here we will use it to implement 3 real work use cases :

Prerequisites

To run the article’s code, you need to have:

– JDK 6 or greater – examples here are in Java 8.
– Disruptor dependency from maven

or from LMAX repository here

1) Ordered building strategy

We want to have a Strategy that build a car, then set color on it, then set power, then set wheels and finally print it, all that stuff in order.

Ordered steps
Ordered steps

 

Easy with the Disruptor DSL : check the code below:

See method DisruptorTests.buildCarInOrder() for more information.

Here a fully working example with all init:

See Class ExampleMain for more information.

2) Parallel building strategy

In this strategy we want our car to be Colored, Powered, Wheeled in parallel, and then Print it.

With this strategy, the Same object “Event-A” will be consumed by 3 threads at the same time.

Parallel
Parallel

 

Let see how with the code looks:

See method DisruptorTests.buildCarInParallel() for complete example.

3) Dispatching car strategy

After building our car, we want to deliver it by using one delivery type at time (we can’t deliver the same car using multiple ways 😉 ).

 

dispatcher
dispatcher

For this strategy, we use a WorkPool and define our WorkHandler(business logic executed by each thread) and then build a ringbuffer  :

See method DisruptorTests.dispatchEachCarByOnlyOneWarAtOnce() for complete example.

That it, now you can configure a powerful producer/consumer pattern in a easy way using Disruptor api.

Get the code

The code is available on GitHub.

Hope you enjoyed this post, and don’t forget to share!

About the author

Hichame EL KHALFI is a consultant and a senior software architect on financial Front-to-Back software solution.

You can follow him on twitter @Helkhalfi

Links

Disruptor api home page : http://lmax-exchange.github.io/disruptor/

2 thoughts on “Disrupt java threads.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.