开发者

reading from input stream of socket without busy waiting in JAVA

开发者 https://www.devze.com 2022-12-14 09:16 出处:网络
i want to read from input stream when i connect to a server socket. but there may exist some delays between messages how can i 开发者_StackOverflow中文版read from input stream without busy loop ?

i want to read from input stream when i connect to a server socket. but there may exist some delays between messages how can i 开发者_StackOverflow中文版read from input stream without busy loop ?

string mes = socketReader.readLine();

above line returns null when no input provided in socket input stream. i want to be notified somehow when a message is ready in input stream.

tnx


Have you looked asynchronous IO?


In a GUI context, SwingWorker may help: let doInBackground() do the reading, use process() for interim reuslts, and done() to wrap up. You can register a PropertyChangeListener to drive a progress indicator. This article expands on the idea, and a back port to Java 1.5 is available. Here's a simple example that reads from a JDBC source instead of a stream, but the idea is the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消