Java服务器-Disruptor使用注意

最近看了一下部署后台的服务器状况,发现我的一个Java程序其占用的CPU时长超过100%,排查后发现竟是Disruptor引起的,让我们来看看究竟为什么Disruptor会有这样的表现。

发现占用CPU时间超过100%的进程

首先是在服务器上用top命令查看服务器状态,发现有一个应用程序占用的CPU时长超过100%,如图:

我根据进程号查了一下,发现是我的一个Java游戏后台服务,有一个CPU几乎被占满,因此继续排查究竟是什么代码导致了这种情况。

top -Hp 27538将这个进程的所有线程显示出来,按照CPU占用时间排序,看到了这个结果:

 

27658线程占用了近乎所有的CPU时间,而且一直都是,因此查看这个进程的详细信息。

jstack pid > pid.log命令将该进程的进程快照输出到一个文件中,下载下来。

 将27658转换为16进制0x6c0a后在线程快照中查询(因为线程快照中线程ID都是16进制存放,所以需要转换)

"disruptor-0" #27 prio=5 os_prio=0 tid=0x00007fa100c58000 nid=0x6c0a runnable [0x00007fa0ae080000]
   java.lang.Thread.State: RUNNABLE
    at com.lmax.disruptor.BusySpinWaitStrategy.waitFor(BusySpinWaitStrategy.java:39)
    at com.lmax.disruptor.ProcessingSequenceBarrier.waitFor(ProcessingSequenceBarrier.java:56)
    at com.lmax.disruptor.BatchEventProcessor.processEvents(BatchEventProcessor.java:159)
    at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:125)
    at java.lang.Thread.run(Thread.java:748)

这是Disruptor的一个堆栈,为了更直观地查看线程的状态信息

分析Disruptor为何会占用整个CPU

根据上面快照的分析,实际是Disruptor的等待策略相关的线程所导致的,查看BusySpinWaitStrategy类,发现有相关说明

 * This strategy will use CPU resource to avoid syscalls which can introduce latency jitter.  It is best
 * used when threads can be bound to specific CPU cores.

现在终于知道了,原来是因为这个策略就是让线程绑定了一个CPU核心,自然其CPU占用时间就超过100%了

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java Disruptor 代码示例: ```java import com.lmax.disruptor.RingBuffer; import com.lmax.disruptor.dsl.Disruptor; import java.nio.ByteBuffer; import java.util.concurrent.Executor; import java.util.concurrent.Executors; public class LongEvent { private long value; public void set(long value) { this.value = value; } } class LongEventFactory implements EventFactory<LongEvent> { public LongEvent newInstance() { return new LongEvent(); } } class LongEventHandler implements EventHandler<LongEvent> { public void onEvent(LongEvent event, long sequence, boolean endOfBatch) { System.out.println("Event: " + event); } } public class DisruptorExample { public static void main(String[] args) { Executor executor = Executors.newCachedThreadPool(); LongEventFactory factory = new LongEventFactory(); int bufferSize = 1024; Disruptor<LongEvent> disruptor = new Disruptor<>(factory, bufferSize, executor); disruptor.handleEventsWith(new LongEventHandler()); RingBuffer<LongEvent> ringBuffer = disruptor.start(); ByteBuffer bb = ByteBuffer.allocate(8); for (long l = 0; true; l++) { bb.putLong(0, l); ringBuffer.publishEvent(new EventTranslatorOneArg<LongEvent, ByteBuffer>() { public void translateTo(LongEvent event, long sequence, ByteBuffer arg0) { event.set(arg0.getLong(0)); } }, bb); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 该代码使用 LMAX Disruptor 实现了一个简单的事件处理程序。Disruptor 可以效地处理并发事件处理任务,例如性能日志记录、消息传递和网络通信。在这个例子中,我们定义了一个事件对象 LongEvent,包含一个 long 值。我们使用 LongEventFactory 为 Disruptor 创建新的事件对象,使用 LongEventHandler 处理事件。我们还定义了一个 ByteBuffer,用于在主线程中创建事件并将其发布到 Disruptor 中。在主线程中,我们每秒钟创建一个新事件并将其发布到 Disruptor 中。在事件处理程序中,我们简单地打印事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值