Time out for InetAddress.getHostName()

jerch
jerch used Ask the Experts™
on
How can you implement a time-out mechanism on InetAddress.getHostName()?

I've tried implementing a timeout thread which calls the interrupt() on the thread executiung InetAddress.getHostName() but the getHostName() doesn't throw any InterruptedException.

Thanks.

Jerson
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I've solved the problem. How can I delete this?
Mick BarryJava Developer
Top Expert 2010

Commented:
Care to share the solution with us?

Author

Commented:
Sure.
This is not the actual code but the concept is the same. Since there's no way to interrupt the getHostName() method, I just ignore the thread executing it and immediately resume from my client code. I know it's not an elegant solution.
If anyone can provide me a better solution and provide thread pooling support for the GetHostNameThread, I will grant him the points.

class GetHostNameThread extends Thread {
     private InetAddress _ip;
     private String _hostName = null;
     
     public GetHostNameThread(InetAddress ip) {
          _ip = ip;
     }
     
     public void run() {
          try {
               _hostName = ip.getHostName();
          } catch (Exception e) {}
     }
     
     public String getHostName() {
          return _hostName;
     }
}

class ClientCode {
     public static void main(String[] args) throws Exception {
          int timeout = 5 * 1000; // 5 seconds
          InetAddress ip = ...;
         
          GetHostNameThread t = new GetHostNameThread(ip);
          t.start();

          t.join(timeout);

          String hostName = t.getHostName();

          if (hostName == null) {
               System.out.println("TIME OUT");
          } else {
               System.out.println(hostName);
          }
     }
}

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Mick BarryJava Developer
Top Expert 2010

Commented:
Damn, that's what I was going to suggest :-)
I couldn't think of a better way.
Does the getHostName() call eventually return?

Author

Commented:
Yup, it does but it takes something like 16 seconds or more, which is not acceptable in our system.
jerch:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- PAQ'd and points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer
Force accepted

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial