您可以使用
wait和
notify方法。
为此,需要有一些全局可访问的对象,该对象此时的程序中其他任何东西都未使用其锁定。我假设列表创建者
Runnable本身可以扮演这个角色。
因此,您可以将以下内容添加到创建列表的
Runnable类中:
private boolean listsDone = false;boolean getListsDone() { return listsDone;}run()在创建列表之后,立即对其方法进行如下操作:
synchronized (this) { listsDone = true; notifyAll();}在其他人
Runnable的
run()方法需要等待的时候,类似这样:
synchronized (listCreatingRunnableObject) { if (!listCreatingRunnableObject.getListsDone()) { try { listCreatingRunnableObject.wait(); } catch (InterruptedException e) { // handle it somehow } }}更新:
为明确起见,两个
synchronized块都需要在同一对象上同步,并且您必须在该对象上调用
wait()和
notifyAll()。如果对象是
Runnable,则它对于第一个对象是隐式的(如上面的代码所示),但是如果是活动对象,则在两种情况下都需要显式使用活动对象。



