且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

线程中断没有结束阻塞调用输入流读取

更新时间:2021-07-17 09:37:52

RXTX SerialInputStream(由serial.getInputStream()调用返回的内容)支持超时方案,最终解决了我所有的问题。在创建新的SerialReader对象之前添加以下内容会导致读取不再无限期地阻塞:

The RXTX SerialInputStream (what is returned by the serial.getInputStream() call) supports a timeout scheme that ended up solving all my problems. Adding the following before creating the new SerialReader object causes the reads to no longer block indefinitely:

serial.enableReceiveTimeout(1000);

在SerialReader对象中,我不得不改变一些东西直接从InputStream读取而不是创建ReadableByteChannel,但现在,我可以毫无问题地停止并重新启动阅读器。

Within the SerialReader object, I had to change a few things around to read directly from the InputStream instead of creating the ReadableByteChannel, but now, I can stop and restart the reader without issue.