blob: 6a3be491f44057bc68d7bf3c2bd39a4f628c2a67 [file] [log] [blame]
tom8bf2e6b2014-09-10 20:53:54 -07001package org.onlab.util;
2
3import org.jboss.netty.util.HashedWheelTimer;
4
5/**
6 * Hashed-wheel timer singleton. Care must be taken to shutdown the timer
7 * only when the VM is ready to exit.
8 */
9public final class Timer {
10
Yuta HIGUCHIf9ce4972014-10-04 22:13:47 -070011 private static volatile HashedWheelTimer timer;
tom8bf2e6b2014-09-10 20:53:54 -070012
13 // Ban public construction
14 private Timer() {
15 }
16
17 /**
18 * Returns the singleton hashed-wheel timer.
19 *
20 * @return hashed-wheel timer
21 */
22 public static HashedWheelTimer getTimer() {
23 if (Timer.timer == null) {
Yuta HIGUCHIf9ce4972014-10-04 22:13:47 -070024 initTimer();
25 }
26 return Timer.timer;
27 }
28
29 private static synchronized void initTimer() {
30 if (Timer.timer == null) {
Yuta HIGUCHIa546afc2014-10-06 21:44:48 -070031 HashedWheelTimer hwTimer = new HashedWheelTimer();
32 hwTimer.start();
33 Timer.timer = hwTimer;
tom8bf2e6b2014-09-10 20:53:54 -070034 }
tom8bf2e6b2014-09-10 20:53:54 -070035 }
36
37}