Breaking on user input without delay in Java?
I have a while loop that reads from a socket, and instead of a timer, I want to break, close the socket and end the program on user input, but I don't want the user to have to enter a "1" or "continue" or "something" for each reading. I just want if there is input from the user, break and close, otherwise, if there is no interference, continue. I am afraid I am going to have to use a timer, which I don't want to do because it would delay receiving from the socket, and I need to have little or no delay if possible.
Is there some Java function that does this or a simple solution I am missing?
EDIT:
I don't want to wait for the user inp开发者_Python百科ut. I just want to continue unless user input is detected, but the point is that there is no delay in checking to see if the user has entered something. I am going on the program assuming that it should simply continue until the user says otherwise.
I think I have worked out that you want to specify a timeout period? You can set a timeout by calling on the Socket the method setSoTimeout(int timeout)
, you can also specify it through the Socket constructor. See Socket API here
Hope this helps.
The best way to do this is probably to start a thread running which awaits user input. if this is detected then the thread can set a flag which is checked inside the loop.
You can do an isReady on the input stream to see if there is any data there. In the simplest case, you could sleep for the specified time, then check isReady, and proceed or abort as required. If you want to respond immediately when the user sends the cancel signal, you could have a loop that keeps checking isReady and checking the current time against a starting time, and breaks when either condition is met. If there's some other work that you want to do while waiting for this to happen, you could put it in a separate thread. (In which case the loop should include a yield() to let other things happen.)
You probably want two threads.
One thread should read user input and put that "command" in a variable that is shared with another thread.
The second thread should process "commands" from that variable (you probably want a queue of commands being built). That thread should wait for commands being placed in the queue.
The variable (probably a queue) holding the commands should be protected by a synchronized block. The second thread should wait() on the object that is being synchronized on and the first thread should run notify() on that object when it has placed a command there.
Then the second thread can process commands and react to them without having to wait for user input (as long as there are commands to process) and be immediately woken up when there is something to do.
You need Threading for your requirement. Open a new thread that reads from socket while in your main thread you wait for user input. When you get the input you can interrupt your other thread.
You can do a Thread class such as:
public class ClientThread extends Thread {
private String address;
private int port;
ClientThread(String address, int port) {
this.address = address;
this.port = port;
}
public void run() {
// connect to socket and start reading
. . .
}
}
And then use it in your application with:
ClientThread c = new ClientThread("127.0.0.1", 4547);
c.start();
How about binding a event listener to your input, and if there is a input you handle it(in this case close the socket connection)
You have a "similar" sample here
EDIT: Didnt fully read your question. Thought you hade some kind of GUI. Since you dont then you coudl do something like this (havent use threads in a while in Java so this is "pseudocode" but i hope you get tthe picture):
public void start()
{
Console console = System.console();
if (console != null)
{
Thread th = new Thread(yourWorkerClass);
th.run();
//blocking
String readLine = console.readLine("Do something: ", (Object[])null);
th.stopYourWorkMethod();
}
}
精彩评论