Java异步方法调用

发布于 2024-09-29 11:00:14 字数 409 浏览 3 评论 0原文

我已经有一个线程必须执行以下工作:

public class DetectionHandler extends TimerTask {

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

我希望扫描是并行的。所以我假设我必须异步调用这两个方法。扫描完成后,我可以继续在DetectionHandler 类中工作。

我尝试过BluetoothModule和WiFiModule实现Runnable的方式,但没有运气。总氮

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-10-06 11:00:14

使用 ExecutorService 你可以编写像这样的东西:

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);

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);
感悟人生的甜 2024-10-06 11:00:14

Executors 获取 ExecutorService并给它一个 FutureTask

然后,您可以通过在返回的 Future 上调用阻塞 get() 来等待结果。扫描将并行运行,但您的运行方法(此处所示)仍将等待扫描完成。

有点像:

     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

但要小心, get 将返回任何调用返回的内容。异常被包装在 ExecutionException 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文