Disruptor Java版和.NET版的区别

The main differences comes from the fact that .NET supports structs (value types), Java doesn't.

In Java, every entry (or message) exchanged by the disruptor needs to inherit from a base class called AbstractEntry. This class exposes the Sequence number required by the disruptor to process the messages, it is basically a header for the message.

In .NET we have replaced AbstractEntry by a generic struct: Entry<T>. An entry contains 2 fields: the sequence number and a field called data, used to store the message (of type T). Using this struct has several advantages:

  • the array in the RingBuffer is of type Entry<T>, when we need to access the Sequence number we don't have to dereference (Entry<T> is a struct so instances are directly nested in the array) and this improves cache spatial locality.
  • your message types do not need to implement or inherit from a base class, you can use POCOs.

最主要的区别是.NET支持结构类型(结构类型是值类型),而Java不支持。

在Java中,每一个被Disruptor交换的条目(或者消息)需要继承AbstractEntry基类。这个类提供了Disruptor处理消息时必须的序列号。它是消息头中最基本的信息。

在.NET中用泛型结构Entry<T>代替了Java中的AbstractEntry,一个条目包含两个域:一个是序列号,另一个是数据域,用来存储消息(T类型)。用结构有如下几个优点:

  • RingBuffer中的数组是Entry<T>类型,当我们需要访问序列号时,不需要解引用,并且能够提高缓存空间的局部性。
  • 你的消息类型不需要实现或者继承基类,你可以使用POCOs.

你可能感兴趣的:(disruptor)