aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/concurrent/Once.java
blob: 2e717f16d0e97da0a6c12197f00b092d9dac85ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.concurrent;

import java.time.Duration;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Execute a runnable exactly once in a background thread.
 *
 * @author mpolden
 */
public class Once extends TimerTask {

    private static final Logger log = Logger.getLogger(Once.class.getName());

    private final Runnable runnable;
    private final Timer timer = new Timer(true);

    // private to avoid exposing run method
    private Once(Runnable runnable, Duration delay) {
        this.runnable = Objects.requireNonNull(runnable, "runnable must be non-null");
        Objects.requireNonNull(delay, "delay must be non-null");
        timer.schedule(this, delay.toMillis());
    }

    /** Execute runnable after given delay */
    public static void after(Duration delay, Runnable runnable) {
        new Once(runnable, delay);
    }

    @Override
    public void run() {
        try {
            runnable.run();
        } catch (Throwable t) {
            log.log(Level.WARNING, "Task '" + runnable + "' failed", t);
        } finally {
            timer.cancel();
        }
    }

}