I have a server communicating over network interface. This server should run on a background thread, so that it does not block the ui thread. The ui thread starts and stops the server. Even starting and stopping should be done from a background thread.
Currently I wrap all public methods of the server class in a thread that gets started immediately:
class Server {
public void start() {
new Thread() {
public void run() {
handleStart();
}
}.start();
}
public void stop() {
new Thread() {
public void run() {
handleStop();
}
}.start();
}
private void handleStart() {
startLooperThread();
}
private void handleStop() {
stopLooperThread();
}
}
Is this the proper way to do this or are there any neat alternatives provided by JDK (or any other library)?