建议你
- 创建一个对象来封装“动画”生命周期
- 在对象中,您将有一个线程或一个计时器
- 提供
start()
动画的方法和awaitCompletion()
- 使用
private final Object completionMonitor
字段跟踪完成synchronize
情况,并使用wait()
和notifyAll()
协调awaitCompletion()
程式码片段:
final class Animation { final Thread animator; public Animation() { animator = new Thread(new Runnable() { // logic to make animation happen }); } public void startAnimation() { animator.start(); } public void awaitCompletion() throws InterruptedException { animator.join(); }}您也可以将用作
ThreadPoolExecutor单线程或
ScheduledThreadPoolExecutor,并将动画的每个帧捕获为
Callable。提交
Callables
的序列,并使用
invokeAll()或a
CompletionService阻止您感兴趣的线程,直到动画完成。



