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í <[email protected]>
> 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.
> 

-- 
PGP fingerprint: 5E61 C878 0F86 295C E17D  8677 9F0F E587 374B BE81
https://pgp.mit.edu/pks/lookup?op=vindex&search=0x9F0FE587374BBE81
_______________________________________________
List info: https://lists.mayfirst.org/mailman/listinfo/guardian-dev
To unsubscribe, email:  [email protected]

Reply via email to