Tag Archives: java

Circular queues in Java

I’d like to see how deep the rabbit’s hole is, starting it with a quick round of Java queues. The most important one in low latency application context is the circular queues or ring buffers. These are the same. It’s a fixed size data buffer, connecting the end to the beginning creating a nice infinitive data ring, so your hamster could easily run around and around and around…

Another important architectural point of circular buffer is that it’s a FIFO (First In First Out) construction, easy data piping.

Major points of a low latency application:

  1. Lock Free
  2. Wait Free

Circular Buffer

Some major implementations

  • Fast Flow: FastFlow is a parallel programming framework for multicore platforms, which uses hybrid single-producer/single-consumer queues as a base underlying component for inter-thread communication
  • Thompson queue
  • Lamport queue: http://en.wikipedia.org/wiki/Lamport’s_bakery_algorithm

Basic Implementation

A possible implementation of this queue in Java is using an array based solution. It needs to enqueue the new items into the structure and to dequeue from there. It also needs to shift the data to the dequeued space in the array. Because it has always a fixed array size, it needs a starting array size and the implementation must be able to expand the size if it’s necessary.

Tagged , ,

Building a Trade Exchange site – phase one: check-list of a low-latency applications

I’m thinking about building a demo TX site, playing around low-latency problems with Java. I need to make some researches for this journey so I decided to create a development check-list what are the main points of a low-latency web application. I won’t really deal with front-end side, I’ll create a RESTful API, that will be enough. It should be a websocket driven communication form.

Check-list:

  • using non blocking multi-threaded technologies
  • avoid the complex and unnecessary frameworks (plain jdbc vs JPA?)
  • reducing memory usage
  • check java 8 related parts
  • using a highly responsive server (Netty)
Tagged , , , ,

Using Comparator Interface to sort an object list

The Comparator interface exists in JDK since version 1.2 so it’s a mature part of Java tool box. And it’s a useful Interface. It provides two methods: compare and equals. We can use these methods when we’d like to sort an Array or Collection. Wicked.

So let’s sort a List with full of Object references.

Need a POJO like this:

Beer
-name
-strong

And the task is we’d like to have the strongest beer because we haven’t got enough time so you know. This case we need a Comparator for the stength of beer:

class BeerStrongComparator implements Comparator<Beer> {

  public int compare(Beer b1, Beer b2) {
    return b1.getStrong() < b2.getStrong() ? -1
     : b1.getStrong() > b2.getStrong() ? 1 : 0;
  }

}

And finally the usage:

  Collections.sort(allBeersList, new BeerStrongComparator());

You just put the Comparator as a second parameter in Collections class sort static method.

Tagged , ,