High CPU usage through context switches in Akka application High CPU usage through context switches in Akka application multithreading multithreading

High CPU usage through context switches in Akka application


The problem is that Fazecast's jSerialComm library has a number of different time-out modes.

static final public int TIMEOUT_NONBLOCKING = 0x00000000;static final public int TIMEOUT_READ_SEMI_BLOCKING = 0x00000001;static final public int TIMEOUT_WRITE_SEMI_BLOCKING = 0x00000010;static final public int TIMEOUT_READ_BLOCKING = 0x00000100;static final public int TIMEOUT_WRITE_BLOCKING = 0x00001000;static final public int TIMEOUT_SCANNER = 0x00010000;

Using the non blocking read() method (TIMEOUT_NONBLOCKING) results in a very high CPU usage when combined with the Akka Stream's InputStreamPublisher. To prevent this simply use TIMEOUT_READ_SEMI_BLOCKING or TIMEOUT_READ_BLOCKING.