Hello. Using a thread i am trying to create a socket. That is Socket mySocket = new Socket(...); At times socket creation is taking a long time. I wish to put a timer on it. Say if it takes more than 'n' seconds, i want the thread to terminate. But the thread is blocking on the socket creation and i am finding it difficult to come out of it. For that too i had to use deprecated stop() method from the main() method to come out. Are there any more elegant ways of doing it? Prasad
Originally posted by MSDPRASAD: Hello. Using a thread i am trying to create a socket. That is Socket mySocket = new Socket(...); At times socket creation is taking a long time. I wish to put a timer on it. Say if it takes more than 'n' seconds, i want the thread to terminate. But the thread is blocking on the socket creation and i am finding it difficult to come out of it. For that too i had to use deprecated stop() method from the main() method to come out. Are there any more elegant ways of doing it? Prasad
Nagalinga Murthy
Greenhorn
Joined: Sep 26, 2000
Posts: 2
posted
0
One solution is Class { public static void main(String[] a) { while(1){ Create and start SocketThread;} } SocketThread { boolean socketIsOk; 1.) start thread 1 2.) start thread 2 } Thread 1 { 1.)sockIsOk = false 2.)create a new socket connection here 3.)If no exception then sockIsOk = true } Thread 2 { 1.)wait for 'n' seconds 2.)if sockIsOk = false then stop Thread 1 } }
Originally posted by MSDPRASAD: Hello. Using a thread i am trying to create a socket. That is Socket mySocket = new Socket(...); At times socket creation is taking a long time. I wish to put a timer on it. Say if it takes more than 'n' seconds, i want the thread to terminate. But the thread is blocking on the socket creation and i am finding it difficult to come out of it. For that too i had to use deprecated stop() method from the main() method to come out. Are there any more elegant ways of doing it? Prasad
MSD PRASAD
Greenhorn
Joined: Sep 25, 2000
Posts: 2
posted
0
Thanx Murthy! I still needed to use the stop() method which is deprecated. Various options like polling the socket thread and then throwing an exception on timeout , setting socket thread as daemon thread etc. have been exhausted. Buck stops right at the new Socket();! I am still on look-out for a soluttion. Prasad