blob: fe50883968c677080d8447ce7ca15c373923f81f [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
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
18import org.jboss.netty.util.HashedWheelTimer;
19
20/**
21 * Hashed-wheel timer singleton. Care must be taken to shutdown the timer
22 * only when the VM is ready to exit.
23 */
24public final class Timer {
25
Yuta HIGUCHIf9ce4972014-10-04 22:13:47 -070026 private static volatile HashedWheelTimer timer;
tom8bf2e6b2014-09-10 20:53:54 -070027
28 // Ban public construction
29 private Timer() {
30 }
31
32 /**
33 * Returns the singleton hashed-wheel timer.
34 *
35 * @return hashed-wheel timer
36 */
37 public static HashedWheelTimer getTimer() {
38 if (Timer.timer == null) {
Yuta HIGUCHIf9ce4972014-10-04 22:13:47 -070039 initTimer();
40 }
41 return Timer.timer;
42 }
43
44 private static synchronized void initTimer() {
45 if (Timer.timer == null) {
Yuta HIGUCHIa546afc2014-10-06 21:44:48 -070046 HashedWheelTimer hwTimer = new HashedWheelTimer();
47 hwTimer.start();
48 Timer.timer = hwTimer;
tom8bf2e6b2014-09-10 20:53:54 -070049 }
tom8bf2e6b2014-09-10 20:53:54 -070050 }
51
52}