Basically, I call BufferedReader.ReadLine (); However, I am on a multi-threaded server, where I synchronize the node in the tree. Therefore, when this ReadLine function is called, if someone else reaches node, they are locked. I can’t understand how to make timelimit for the amount of time ReadLine waits for a response before exiting the stream. The closest I got is to create a new thread that will sleep for 1 ms and then check if the variable that I set ReadLine has changed. So something like this:
synchronized (pointer) {
String answer = "";
Thread d = new Thread(new Runnable() {
public void run() {
try {
int i = 0;
while (answer.equals("")) {
if (i == 10000) {
System.out.println("Timeout Occured");
System.exit(0);
}
try {
Thread.sleep(1);
i++;
}
catch(Exception e) {
System.out.println("sleep problem occured");
}
}
}
catch (IOException ex) {
}
}
});
d.start();
answer = socketIn.readLine();
}
This did what I wanted, but I could not figure out how to stop the current thread in order to unlock the node so that other users could continue and not kill the whole server. Finally, I thought maybe I could do this:
Thread d = new Thread(new Runnable() {
public void run() {
try {
answer = socketIn.readLine();
} catch (IOException ex) {
}
}
});
d.join(10000);
catch (InterruptedException e){
socketOut.println("Timeout Occured. Returning you to the beginning...");
socketOut.flush();
return;
}
. - ? , ?
ExecutorService , . ? ?
[EDIT] socketIn BufferedReader, , . , telnet, , .
, , - , . node,