This may be overly simplistic (as it doesn't deal with how to "re-join" the threads after connection is complete - assuming you need to do that.)
Also, if this is going to happen often, you want to use an Executor
(thread pool) instead of manually creating your own Thread
- thread creation/destruction is expensive.
I also neglect exception handling in this snippet (which isn't completely trivial.)
Runnable runnable = new Runnable() {
public void run() {
InetAddress serverAddr = InetAddress.getByName(serverAddress);
String hostname = serverAddr.getCanonicalHostName();
Socket socket = new Socket(new InetSocketAddress(serverAddr, portNumber), 3000);
/* ... do more of your after-connection processing here, assuming it doesn't
* need to be in the original "dispatch" thread.
*/
}
};
Thread t = new Thread(runnable);
t.start();