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í - [email protected] - http://mvdan.cc/ PGP: A9DA 13CD F7A1 4ACD D3DE E530 F4CA FFDB 4348 041C
pgp1wYRk96xEC.pgp
Description: PGP signature
_______________________________________________ List info: https://lists.mayfirst.org/mailman/listinfo/guardian-dev To unsubscribe, email: [email protected]
