views:

48

answers:

3

Hi,

I'm designing a system in java which utilizes a dns lookup class.

My question is, when calling the class's dnsLookup(), whether to do it in a new thread or use the observer pattern and let the dns class tell me when it's done.

This isn't a problem as long as the lookup returns a value almost instantly but when it takes a few seconds (when it doesn't get a response), I don't want to freeze the GUI while waiting.

So, new thread or observer. Appreciate some good links on the subjects as well.

Thanks beforehand - Dennis

A: 

Your GUI is an event driver system so asynchronous notifications are good.

On the other hand, it's a lot easier to do network I/O (particularly if it's just a single DNS lookup) if you use synchronous (blocking) network calls.

Hence I would tend to go for the separate thread option, but then have that thread notify the main GUI thread when it's done.

Alnitak
+3  A: 

You will have to employ both the observer pattern and more than one thread. There's no way to have the DNS invoking callback method in the same thread.

Boris Pavlović
+1. We have an app doing quite a lot of DNS lookups, especially at startup. We use the observer pattern and let a thread pool handle the lookups.
volley
A: 

Since it's a GUI that is making the call, I think it's best that you off-load the call to a different string. In fact, you want to make sure that you're not using the AWT-Thread to make a call that is blocking the GUI from refreshing. I would suggest using something like an ExecutorService to execute your commands and then upon the return, use SwingUtilities and call the invokeLater(Runnable doRun) method to update the GUI with the response.

John Engelman