【发布时间】:2016-05-03 05:11:49
【问题描述】:
我有一个ThreadPoolExecuter 可以同时运行多个线程。在Runnable,我想在另一个线程中运行方法,所以我在执行方法中创建了另一个线程(线程A),现在我想获得线程A的结果以在执行器线程中运行。
让我用一个样本来澄清一下:
ThreadPoolExecuter threadPool = Executors.newFixedThreadPool(5);
threadPool.execute(new Runnable() {
@Override
public void run() {
// do something in threadPool thread
// call method in thread A
getInfo(new QueryExecutorInterface() {
@Override
public void onPostExecute(Cursor cursor) {
// do other thing in threadPool thread again.
}
});
}
});
QueryExecutorInterface 是我想传递给线程A 并在ThreadPool 线程上获得结果的接口。当我像以下方法一样调用侦听器回调时,我在线程A 中得到结果:
class A extend Thread {
@Override
public void run() {
// do something in thread A.
queryExecutorInterface.onPostExecute(cursor);
}
}
PS:我可以使用ReentrantLock 类而不是使用线程A 来解决这种情况。但由于我在此之上还有另一层,所以我不想使用锁定。
【问题讨论】:
标签: java android multithreading