2 * Copyright (c) 2017 Ericsson Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netvirt.sfc.classifier.utils;
11 import java.util.concurrent.Executor;
12 import java.util.concurrent.atomic.AtomicReference;
15 * An executor that only executes the last submitted task. Ongoing tasks wont
18 public class LastTaskExecutor implements Executor {
20 private final Executor executor;
21 private final AtomicReference<Runnable> lastTask = new AtomicReference<>();
23 public LastTaskExecutor(Executor executor) {
24 this.executor = executor;
28 public void execute(final Runnable newTask) {
29 if (newTask == null) {
30 throw new NullPointerException();
33 lastTask.set(newTask);
34 executor.execute(() -> {
35 final Runnable runTask = lastTask.getAndSet(null);
36 if (runTask != null) {