[guardian-dev] Fwd: libaccesspoint concurrency help
Daniel Martí
mvdan at mvdan.cc
Tue May 12 15:49:13 EDT 2015
This looks promising, thank you Michael! Will test it out :)
On Tue, May 12, 2015 at 20:45:47 +0100, Michael Rogers wrote:
> You could use a CountDownLatch to manage the counter, but I'd probably
> do something like this instead:
>
> public void getReachableClients(final ReachableClientListener listener,
> final int timeout) {
> List<Client> clients = getClients();
> if(clients == null) return;
> Executor executor = Executors.newCachedThreadPool();
> for(final Client c : clients) {
> executor.submit(new Runnable() {
> public void run() {
> try {
> InetAddress ip = InetAddress.getByName(c.IPAddr);
> if(ip.isReachable(timeout))
> listener.onReachableClient(c);
> } catch(IOException e) {
> Log.e(TAG, "", e);
> }
> }
> });
> }
> }
>
> This will return the results to the listener on background threads. If
> you want them returned on the UI thread, do this:
>
> if(ip.isReachable(timeout)) {
> runOnUiThread(new Runnable() {
> public void run() {
> listener.onReachableClient(c);
> }
> });
> }
>
> Incidentally, the phrase to google for is "java concurrency in
> practice". I'm not a big collector of programming books by any means,
> but that one is really worthwhile.
>
> Cheers,
> Michael
--
Daniel Martí - mvdan at mvdan.cc - http://mvdan.cc/
PGP: A9DA 13CD F7A1 4ACD D3DE E530 F4CA FFDB 4348 041C
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.mayfirst.org/pipermail/guardian-dev/attachments/20150512/76cab8fe/attachment.sig>
More information about the guardian-dev
mailing list