简体   繁体   中英

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 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

您是否看过异步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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM