A
Alexander Farber
Hello,
I have a probably simple question, but lack Java experience myself.
I'm programming a multi-player card game applet, talking to
an Apache module. (http://preferans.de , is in Russian though).
My problem is: if I will call something like
url = new URL("http", getCodeBase().getHost(),
"/my_module" + some_changing_params);
conn = (HttpURLConnection) url.openConnection();
BufferedReader input = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
process_response_and_update_GUI(line);
}
input.close();
from the actionPerformed(), then my applet will keep freezing.
So I need a separate Thread.
But what exactly should I do in that separate Thread?
Keep polling an "url" variable, and once it's not null (set by
the actionPerformed()) run the code above, then update
GUI and set "url" back to null?
I think polling is not the best way. If I do it constantly, then
my applet will eat CPU. And if I add a Thread.sleep(5000)
inbetween to save some CPU, then the GUI will be not so
responsive (it isn't already because of the HTTP stuff)
Any suggestions please?
Regards
Alex
I have a probably simple question, but lack Java experience myself.
I'm programming a multi-player card game applet, talking to
an Apache module. (http://preferans.de , is in Russian though).
My problem is: if I will call something like
url = new URL("http", getCodeBase().getHost(),
"/my_module" + some_changing_params);
conn = (HttpURLConnection) url.openConnection();
BufferedReader input = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
process_response_and_update_GUI(line);
}
input.close();
from the actionPerformed(), then my applet will keep freezing.
So I need a separate Thread.
But what exactly should I do in that separate Thread?
Keep polling an "url" variable, and once it's not null (set by
the actionPerformed()) run the code above, then update
GUI and set "url" back to null?
I think polling is not the best way. If I do it constantly, then
my applet will eat CPU. And if I add a Thread.sleep(5000)
inbetween to save some CPU, then the GUI will be not so
responsive (it isn't already because of the HTTP stuff)
Any suggestions please?
Regards
Alex