您也可以像这样使用JDK的标准RunnableFuture:
RunnableFuturetask = new FutureTask<>(runnable, null); runOnUiThread(task); try { task.get(); // this will block until Runnable completes } catch (InterruptedException | ExecutionException e) { // handle exception }
myRunnable.wait()
将释放锁定myRunnable
并等待通知
我们总是在等待前添加支票.
//synchronized wait block while(myRunnable.needWait){ myRunnable.wait(); } //synchronized notify block this.needWait = false; myRunnable.notify();