How to asynchronously read stdin?

前端 未结 5 600
遥遥无期
遥遥无期 2021-01-14 05:23

Is there an elegant way to fire an event when characters are available from System.in? I\'d like to avoid polling InputStream.available().

5条回答
  •  青春惊慌失措
    2021-01-14 06:24

    Very generally speaking:

    If you already have an event reactor running, create a thread and have it block on read(). When there's data available, have that thread enqueue an event for the reactor to process. If you can't do this, most event reactors provide an InvokeLater, or CallLater method, for you to run some code in the event processing thread.

    After notifying or scheduling a function call, go back to blocking on read().

提交回复
热议问题