[guardian-dev] Fwd: libaccesspoint concurrency help

Michael Rogers michael at briarproject.org
Tue May 12 15:45:47 EDT 2015


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

On 12/05/15 16:54, Hans-Christoph Steiner wrote:
> 
> I haven't done much fancy concurrency, but one idea that came to mind is this:
> 
> * have a counter for instances of your probe thread
> * have `synchronized` increment/decrement methods
> * as each thread starts, it increments the counter
> * as each thread finishes, it decrements the counter
> * include a timeout in the thread so they return in a reasonable amount of time
> * have a while loop that quits when the counter is back to 0
> * now you have your result
> 
> .hc
> 
> 
> Nathan of Guardian:
>> Some help for Daniel from F-Droid project? This is a neat bit of code
>> that allows apps to directly setup wifi-tether hotspots, which is part
>> of how we do appswap sharing in F-Droid. 
>>
>> ----- Original message -----
>> From: Daniel Martí <mvdan at mvdan.cc>
>> Subject: libaccesspoint concurrency help
>> Date: Tue, 12 May 2015 15:53:52 +0200
>>
>> Hello guys,
>>
>> So I've been working on libaccesspoint today, got most features up and
>> running: https://github.com/mvdan/libaccesspoint
>>
>> There is only one problem - the list of connected devices is gotten
>> through ARP, which is cached for up to a few minutes, so you may well be
>> getting devices that have been disconnected for a bit.
>>
>> So I'm doing another method for getting clients which are reachable,
>> which seems like a general case and should be in the library.
>> "Reachable" is the best way to implement "currently connected" that I
>> found.
>>
>> Obviously I cannot do the network I/O of trying to resolve each IP
>> address on the UI thread. So ideally, for N ip addresses I would fire up
>> N threads, each would find if the IP address is reachable, and then I
>> would collect the results and construct another list consisting only of
>> the IP addresses which were reachable.
>>
>> Seems easy right? Should be. In Go I could do this with channels and
>> WaitGroups in 20 lines at most. But I'm not used to concurrency in Java
>> and so I've been stuck with this for hours.
>>
>> I'm attaching the diff I currently have. It is very, very wrong. So I'm
>> asking for pointers because google doesn't seem to be able to answer
>> "how to write sane concurrency in Java" for me :)
>>
>> And of course, pull requests welcome if anyone wants to give it a go.
>>
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <http://lists.mayfirst.org/pipermail/guardian-dev/attachments/20150512/87a518e6/attachment.sig>


More information about the guardian-dev mailing list