Android:在UI线程完成操作之前,后台线程可能会阻塞吗?
后台线程是否可以将消息排队到主UI线程的处理程序并阻塞,直到消息得到服务Android:在UI线程完成操作之前,后台线程可能会阻塞吗?,android,multithreading,ui-thread,Android,Multithreading,Ui Thread,后台线程是否可以将消息排队到主UI线程的处理程序并阻塞,直到消息得到服务 这里的上下文是,我希望我的远程服务在其主UI线程之外为每个已发布的操作提供服务,而不是从中接收IPC请求的线程池线程。这应该满足您的需要。它对已知对象使用notify()和wait()使此方法本质上是同步的。run()中的任何内容都将在UI线程上运行,并在完成后将控制权返回到doSomething()。这当然会使调用线程处于休眠状态 public void doSomething(MyObject thing) {
这里的上下文是,我希望我的远程服务在其主UI线程之外为每个已发布的操作提供服务,而不是从中接收IPC请求的线程池线程。这应该满足您的需要。它对已知对象使用
notify()
和wait()
使此方法本质上是同步的。run()
中的任何内容都将在UI线程上运行,并在完成后将控制权返回到doSomething()
。这当然会使调用线程处于休眠状态
public void doSomething(MyObject thing) {
String sync = "";
class DoInBackground implements Runnable {
MyObject thing;
String sync;
public DoInBackground(MyObject thing, String sync) {
this.thing = thing;
this.sync = sync;
}
@Override
public void run() {
synchronized (sync) {
methodToDoSomething(thing); //does in background
sync.notify(); // alerts previous thread to wake
}
}
}
DoInBackground down = new DoInBackground(thing, sync);
synchronized (sync) {
try {
Activity activity = getFromSomewhere();
activity.runOnUiThread(down);
sync.wait(); //Blocks until task is completed
} catch (InterruptedException e) {
Log.e("PlaylistControl", "Error in up vote", e);
}
}
}
我不明白在字符串上调用notify()会做什么?