带Kotlin的LMAX Disruptor:不能使用lambda?

例如在Java中,我可以用LMAX disruptor做到这一点:

Disruptor disruptor = new Disruptor(NetworkEvent::new, 2048, Executors.newSingleThreadedExecutor());

在Kotlin我尝试了这个等价物:

val disruptor = Disruptor({ NetworkEvent() }, 2048, Executors.newSingleThreadExecutor())

但是我迎接这个错误:

在这里输入图像说明

首先,你不需要指定T,kotlin可以推断出它。

其次错误消息说最新错误,Disruptor想要一个EventFactory,但你传递一个简单的lambda。

如果你想传递一个lambda作为SAM接口,有时它需要以接口名称作为lambda的前缀。

尝试以下(未经测试):

 val disruptor = Disruptor(EventFactory { NetworkEvent()}, 2048,....) 
Interesting Posts