public void run() { try { while (true) { Runnable cur; synchronized (q) { while ((cur = q.poll()) == null) q.wait(); } cur.run(); cur = null; } } catch (InterruptedException e) { } }
public void defer(Runnable r) { synchronized (q) { q.add(r); q.notify(); } }