Code example for CopyOnWriteArrayList

0
import java.util.List; 
import java.util.concurrent.CopyOnWriteArrayList; 
import java.util.concurrent.Executor; 
 
public class MockExecutor implements Executor {
    final List<Runnable> actions = new CopyOnWriteArrayList<Runnable>();
 
    public void execute(Runnable command) {
        actions.add(command);
    } 
 
    public void runNow() { 
        while (!actions.isEmpty()) {
            actions.remove(0).run();
        } 
    } 
}