2

I have two statements that call two methods from C++ files in the jni library in my Android app. For example -

x1 = function1();
x2 = function2();

Each of these methods take about 8 seconds to return a value (due to some data processing). My aim is to have these be executed simultaneously and not one after the other (which is causing a delay of 16 seconds).

I tried making two Runnables, but then I realized they get added to the same queue.

I don't want to extend the Thread class because I don't want these function calls to be looped (I need them to be called only when I want)

Is there a solution where I can call them both simultaneously just once and have them return their values at about the same time?

4

2 回答 2

1

您应该查看 Android 的 AsyncTask 类。它提供了一种方法来启动在后台运行的线程,然后在工作完成时提供回调。在这种情况下,两个线程都将在后台运行,因此您必须记住,其余代码将一直运行直到工作完成,除非您告诉主线程等待。

于 2013-10-08T20:46:48.910 回答
0

我们可以使用线程池来做到这一点,无需扩展Thread

ExecutorService pool = Executors.newFixedThreadPool(2);
Future future1 = pool.submit(new Callable() { public Object call() { return function1(); } } );
Future future2 = pool.submit(new Callable() { public Object call() { return function2(); } } );
x1 = future1.get();
x2 = future2.get();
于 2013-10-18T13:50:49.850 回答