reading from input stream of socket without busy waiting in JAVA
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.
精彩评论