blob: af0695ac88ccc5913d4ad5d86e4d452eb9e44851 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom8bf2e6b2014-09-10 20:53:54 -070016package org.onlab.util;
17
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070018import java.util.concurrent.TimeUnit;
19import java.util.function.Supplier;
20
21import com.google.common.base.Suppliers;
22
23import io.netty.util.HashedWheelTimer;
24import io.netty.util.Timeout;
25import io.netty.util.TimerTask;
tom8bf2e6b2014-09-10 20:53:54 -070026
27/**
28 * Hashed-wheel timer singleton. Care must be taken to shutdown the timer
29 * only when the VM is ready to exit.
30 */
31public final class Timer {
32
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070033 private static volatile org.jboss.netty.util.HashedWheelTimer timer;
34
35 private static final Supplier<HashedWheelTimer> TIMER =
36 Suppliers.memoize(HashedWheelTimer::new);
37
tom8bf2e6b2014-09-10 20:53:54 -070038
39 // Ban public construction
40 private Timer() {
41 }
42
43 /**
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070044 * Executes one-shot timer task on shared thread pool.
45 *
46 * @param task timer task to execute
47 * @param delay before executing the task
48 * @param unit of delay
49 * @return a handle which is associated with the specified task
50 */
51 public static Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
52 return TIMER.get().newTimeout(task, delay, unit);
53 }
54
tom8bf2e6b2014-09-10 20:53:54 -070055}