views:

55

answers:

2

I have already one thread that has to do following work:

public class DetectionHandler extends TimerTask {

@Override
public void run() {
bluetoothAddresses = BluetoothModule.scanAddresses();
wiFiAddresses = WiFiModule.scanAddresses();
...//when scanning is finished, continue work
}

I would like that scanning to be parallel. So I assume that I have to call that two methods asynchronously. And when that scanning is finished, then I can continue work in DetectionHandler class.

I've tried the way that BluetoothModule and WiFiModule implements Runnable but had no luck. Tnx

+1  A: 

Get an ExecutorService from Executors and give it a FutureTask.

You can then wait for the results by calling the blocking get() on the returned Future. The scans will run parallel but your run method (shown here) will still wait for the scans to be finished.

A bit like:

     FutureTask<List<Address>> btFuture =
       new FutureTask<List<Address>>(new Callable<List<Address>>() {
         public List<Address> call() {
           return BluetoothModule.scanAddresses();
       }});
     executor.execute(btFuture);

     FutureTask<List<Address>> wfFuture =
       new FutureTask<List<Address>>(new Callable<List<Address>>() {
         public List<Address> call() {
           return WifiModule.scanAddresses();
       }});
     executor.execute(wfFuture);

    btAddresses = btFuture.get(); // blocks until process finished
    wifiAddresses = wfFuture.get(); // blocks

Be carefull though, get will return whatever call returns. Exceptions are wrapped in an ExecutionException.

extraneon
tnak you: I did it like this http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter10/concurrencyTools.html
vale4674
@vale4674 If you decide to play around with threads and start sharing data _between_ threads, I heartilly recommend http://www.javaconcurrencyinpractice.com/ (the book). Using threads can have strange results when not careful. Not a problem in this case of course.
extraneon
+1  A: 

Using ExecutorService you can write something like this:

ArrayList<Callable<Collection<Address>>> tasks = new ArrayList<Callable<Collection<Address>>>();
tasks.add(new Callable<Collection<Address>>() {
  public Collection<Address> call() throws Exception {
    return BluetoothModule.scanAddresses();
  }
});
tasks.add(new Callable<Collection<Address>>() {
  public Collection<Address> call() throws Exception {
    return WiFiModule.scanAddresses();
  }
});

ExecutorService executorService = Executors.newFixedThreadPool(2);
List<Future<Collection<Address>>> futures = executorService.invokeAll(tasks);
Eugene Kuleshov