I'm writing a socket interface and would like it's use to look something like this:
SocketServer server = SocketServer.connect("some server url");
server.on("connect", new SocketEvent() {
@Override
public void onEvent() {
"Do something when socket is connected"
}
}
Now what I would like to happen is whenever the variable "server" falls out of scope, the garbage collection will come around and eventually clean in. It's not super vital that it gets freed that instant. I'm trying to avoid something like, server.close();
What I need to know is how I'm to manage the closing of threads and streams under the hood. I've read that finalize gets called when the garbage collector comes around but everybody is saying not to use finalize. Finalize is where I'm thinking I should handle such things but is there a better way?